forked from nikeee/react-text-loop-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
97 lines (86 loc) · 2.85 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
declare global {
interface Window {
mozRequestAnimationFrame;
oRequestAnimationFrame;
msRequestAnimationFrame;
webkitRequestAnimationFrame: (callback: FrameRequestCallback) => number;
mozCancelRequestAnimationFrame;
webkitCancelRequestAnimationFrame;
oCancelRequestAnimationFrame;
msCancelRequestAnimationFrame;
webkitCancelAnimationFrame: (handle: number) => void;
}
}
declare interface Handle {
value: number | void;
}
const requestAnimFrame = (() => {
if (typeof window !== "undefined") {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function cb(/* function */ callback): void {
window.setTimeout(callback, 1000 / 60);
}
);
}
return (): void => {
/* return empty function */
};
})();
export type RequestTimeout = Record<string, unknown> | number | void | Handle;
/**
* Behaves the same as setTimeout except uses requestAnimationFrame() where possible for better performance
* @param {function} fn The callback function
* @param {int} delay The delay in milliseconds
*/
export const requestTimeout = function (fn, delay: number): RequestTimeout {
if (
!window.requestAnimationFrame &&
!window.webkitRequestAnimationFrame &&
!(
window.mozRequestAnimationFrame && window.mozCancelRequestAnimationFrame
) && // Firefox 5 ships without cancel support
!window.oRequestAnimationFrame &&
!window.msRequestAnimationFrame
) {
return window.setTimeout(fn, delay);
}
const start = new Date().getTime();
const handle: Handle = { value: 0 };
function loop(): number | void {
const current = new Date().getTime();
const delta = current - start;
if (delta >= delay) {
fn.call(null);
} else {
handle.value = requestAnimFrame(loop);
}
}
handle.value = requestAnimFrame(loop);
return handle;
};
/**
* Behaves the same as clearTimeout except uses cancelRequestAnimationFrame() where possible for better performance
* @param {int|object} fn The callback function
*/
export const clearRequestTimeout = function (handle): void {
return window.cancelAnimationFrame
? window.cancelAnimationFrame(handle.value)
: window.webkitCancelAnimationFrame
? window.webkitCancelAnimationFrame(handle.value)
: window.webkitCancelRequestAnimationFrame
? window.webkitCancelRequestAnimationFrame(
handle.value
) /* Support for legacy API */
: window.mozCancelRequestAnimationFrame
? window.mozCancelRequestAnimationFrame(handle.value)
: window.oCancelRequestAnimationFrame
? window.oCancelRequestAnimationFrame(handle.value)
: window.msCancelRequestAnimationFrame
? window.msCancelRequestAnimationFrame(handle.value)
: clearTimeout(handle);
};