Skip to content

Instantly share code, notes, and snippets.

@domasx2
Created November 2, 2015 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domasx2/e97dce67349fb46f01de to your computer and use it in GitHub Desktop.
Save domasx2/e97dce67349fb46f01de to your computer and use it in GitHub Desktop.
Wrap requestAnimationFrame to implement return value & cancelAnimationFrame
const callbacks = new Map();
let id = 0;
const realRequestAnimationFrame = window.requestAnimationFrame;
window.requestAnimationFrame = function(cb) {
id++;
callbacks.set(id, cb);
return id;
}
window.cancelAnimationFrame = function(id) {
callbacks.delete(id);
}
function tick(timestamp) {
realRequestAnimationFrame(tick);
let cbs = Array.from(callbacks.values());
callbacks.clear();
for (let cb of cbs) {
cb(timestamp);
}
}
realRequestAnimationFrame(tick);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment