Skip to content

Instantly share code, notes, and snippets.

@WebInspectInc
Last active December 27, 2015 00:18
Show Gist options
  • Save WebInspectInc/7236209 to your computer and use it in GitHub Desktop.
Save WebInspectInc/7236209 to your computer and use it in GitHub Desktop.
Animation Delay with rAF
function animate (callback, time, oldTime) {
var oldTime = oldTime || new Date().getTime();
var now = new Date();
if (new Date(oldTime + time) < now) {
callback();
oldTime = now.getTime();
}
requestAnimationFrame(function() { animate(callback, time, oldTime); });
}
//useage:
//animate(animateMyThing, 1000);
@WebInspectInc
Copy link
Author

To use this you probably also need the rAF polyfill: http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/

@WebInspectInc
Copy link
Author

...or maybe you don't. Support is pretty good now: http://caniuse.com/#search=requestanimation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment