mirror of
https://github.com/youzan/vant.git
synced 2025-10-20 18:54:24 +00:00
[type] raf (#2699)
This commit is contained in:
33
packages/utils/raf.ts
Normal file
33
packages/utils/raf.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* requestAnimationFrame polyfill
|
||||
*/
|
||||
|
||||
import { isServer } from './index';
|
||||
|
||||
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 || root.webkitRequestAnimationFrame || fallback;
|
||||
|
||||
/* istanbul ignore next */
|
||||
const iCancel = root.cancelAnimationFrame || root.webkitCancelAnimationFrame || root.clearTimeout;
|
||||
|
||||
export function raf(fn: FrameRequestCallback): number {
|
||||
return iRaf.call(root, fn);
|
||||
}
|
||||
|
||||
export function cancel(id: number) {
|
||||
iCancel.call(root, id);
|
||||
}
|
Reference in New Issue
Block a user