forked from youzan/vant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraf.ts
More file actions
40 lines (31 loc) · 855 Bytes
/
raf.ts
File metadata and controls
40 lines (31 loc) · 855 Bytes
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
/**
* requestAnimationFrame polyfill
*/
import { isServer } from '..';
let prev = Date.now();
/* istanbul ignore next */
function fallback(fn: FrameRequestCallback): number {
const curr = Date.now();
const ms = Math.max(0, 16 - (curr - prev));
const id = setTimeout(fn, ms);
prev = curr + ms;
return id;
}
/* istanbul ignore next */
const root = <Window>(isServer ? global : window);
/* istanbul ignore next */
const iRaf = root.requestAnimationFrame || fallback;
/* istanbul ignore next */
const iCancel = root.cancelAnimationFrame || root.clearTimeout;
export function raf(fn: FrameRequestCallback): number {
return iRaf.call(root, fn);
}
// double raf for animation
export function doubleRaf(fn: FrameRequestCallback): void {
raf(() => {
raf(fn);
});
}
export function cancelRaf(id: number) {
iCancel.call(root, id);
}