There’s been quite a bit of copy/pasting of the compatibility shim for requestAnimationFrame
going around on the Net, which is all fine and dandy, but sadly the popular shim isn’t compatible with passing in the timestamp on the setTimeout fallback.
Here’s the improved one:
window.requestAnimationFrame ||= window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || (callback, element) -> window.setTimeout( -> callback(+new Date()) , 1000 / 60)
Also, a JavaScript version for those of you who have suffered the misfortune of not choosing to use CoffeeScript.
window.requestAnimationFrame || (window.requestAnimationFrame = window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback, element) { return window.setTimeout(function() { callback(+new Date()); }, 1000 / 60); });
Take notice of the param passed to the callback +new Date()
. It’s the timestamp that leading implementations pass in.
It has also been rumored that Chrome10 doesn’t pass in the timestamp either, so for super reliability you’ll want to have timestamp ||= +new Date()
as the first line of your callback as well.
There were also several issues mentioned in this gist which have not been decisively resolved, so any feedback is certainly welcome. I also decided to just polyfill that ‘ish, because it seems legit enough.
Good luck and Godspeed.
Thx for publishing this.. my shim was pretty basic and the more robust polyfill is smarter:
https://gist.github.com/1579671
Anyway, the spec is changing so this argument wont be correct anymore:
http://lists.w3.org/Archives/Public/public-web-perf/2012May/0061.html
Will be publishing a revision soon..
LikeLike