From b6cd212a772ba81aea29f9bcb067dbaab4e886a7 Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 22 Sep 2021 16:16:48 +0800 Subject: [PATCH] perf(@vant/use): remove raf polyfill (#9544) --- packages/vant-use/src/utils.ts | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/packages/vant-use/src/utils.ts b/packages/vant-use/src/utils.ts index 2bc2417b5..d30edf8f7 100644 --- a/packages/vant-use/src/utils.ts +++ b/packages/vant-use/src/utils.ts @@ -1,25 +1,13 @@ export const inBrowser = typeof window !== 'undefined'; -const root = (inBrowser ? window : global) as Window; - -let prev = Date.now(); - -function rafPolyfill(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; -} - export function raf(fn: FrameRequestCallback): number { - const requestAnimationFrame = root.requestAnimationFrame || rafPolyfill; - return requestAnimationFrame.call(root, fn); + return inBrowser ? requestAnimationFrame(fn) : -1; } export function cancelRaf(id: number) { - const cancelAnimationFrame = root.cancelAnimationFrame || root.clearTimeout; - cancelAnimationFrame.call(root, id); + if (inBrowser) { + cancelAnimationFrame(id); + } } // double raf for animation