Skip to content

Commit f272d40

Browse files
committed
fix: Raf should not crash by window not defined
1 parent 461ee5a commit f272d40

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/raf.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
let raf = (fn: () => void) => +setTimeout(fn, 16);
1+
let raf = (callback: FrameRequestCallback) => +setTimeout(callback, 16);
22
let caf = (num: number) => clearTimeout(num);
33

4-
if (typeof window !== 'undefined') {
5-
raf = requestAnimationFrame;
6-
caf = cancelAnimationFrame;
4+
if (typeof window !== 'undefined' && 'requestAnimationFrame' in window) {
5+
raf = (callback: FrameRequestCallback) =>
6+
window.requestAnimationFrame(callback);
7+
caf = (handle: number) => window.cancelAnimationFrame(handle);
78
}
89

910
export default function wrapperRaf(callback: () => void): number {

0 commit comments

Comments
 (0)