Skip to content

Commit

Permalink
fix: Raf should not crash by window not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Aug 13, 2020
1 parent 461ee5a commit f272d40
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/raf.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
let raf = (fn: () => void) => +setTimeout(fn, 16);
let raf = (callback: FrameRequestCallback) => +setTimeout(callback, 16);
let caf = (num: number) => clearTimeout(num);

if (typeof window !== 'undefined') {
raf = requestAnimationFrame;
caf = cancelAnimationFrame;
if (typeof window !== 'undefined' && 'requestAnimationFrame' in window) {
raf = (callback: FrameRequestCallback) =>
window.requestAnimationFrame(callback);
caf = (handle: number) => window.cancelAnimationFrame(handle);
}

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

0 comments on commit f272d40

Please sign in to comment.