crootfast
Version:
大前端工程化命令行脚手架
40 lines (32 loc) • 835 B
JavaScript
export function requestInterval(callback, delay) {
let start = performance.now();
let handle = {};
function loop(currentTime) {
handle.value = requestAnimationFrame(loop);
if (currentTime - start >= delay) {
callback();
start = currentTime;
}
}
handle.value = requestAnimationFrame(loop);
return handle;
}
export function clearRequestInterval(handle) {
cancelAnimationFrame(handle.value);
}
export function requestTimeout(callback, delay) {
let start = performance.now();
let handle = null;
function loop(currentTime) {
if (currentTime - start >= delay) {
callback();
} else {
handle = requestAnimationFrame(loop);
}
}
handle = requestAnimationFrame(loop);
return handle;
}
export function clearRequestTimeout(handle) {
cancelAnimationFrame(handle);
}