38 lines
863 B
JavaScript
38 lines
863 B
JavaScript
// @flow
|
|
|
|
// Animation frame based implementation of setTimeout.
|
|
// Inspired by Joe Lambert, https://gist.github.com/joelambert/1002116#file-requesttimeout-js
|
|
|
|
const hasNativePerformanceNow =
|
|
typeof performance === 'object' && typeof performance.now === 'function';
|
|
|
|
const now = hasNativePerformanceNow
|
|
? () => performance.now()
|
|
: () => Date.now();
|
|
|
|
export type TimeoutID = {|
|
|
id: AnimationFrameID,
|
|
|};
|
|
|
|
export function cancelTimeout(timeoutID: TimeoutID) {
|
|
cancelAnimationFrame(timeoutID.id);
|
|
}
|
|
|
|
export function requestTimeout(callback: Function, delay: number): TimeoutID {
|
|
const start = now();
|
|
|
|
function tick() {
|
|
if (now() - start >= delay) {
|
|
callback.call(null);
|
|
} else {
|
|
timeoutID.id = requestAnimationFrame(tick);
|
|
}
|
|
}
|
|
|
|
const timeoutID: TimeoutID = {
|
|
id: requestAnimationFrame(tick),
|
|
};
|
|
|
|
return timeoutID;
|
|
}
|