Created
October 1, 2011 12:49
-
-
Save KrofDrakula/1256023 to your computer and use it in GitHub Desktop.
Polyfills the `requestAnimationFrame` function for all browsers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(context) { | |
var exts = ['', 'webkit', 'moz', 'o', 'ms']; | |
function lookup(name) { | |
var n, i; | |
for (i in exts) { | |
n = lcfirst(exts[i] + ucfirst(name)); | |
if (context[n]) return n; | |
} | |
return false; | |
} | |
function lcfirst(str) { return str[0].toLowerCase() + str.slice(1); } | |
function ucfirst(str) { return str[0].toUpperCase() + str.slice(1); } | |
function polyfill(name, func) { | |
var _native = lookup(name); | |
if (!_native) context[name] = func; | |
else context[name] = context[_native]; | |
} | |
// ----[ requestAnimationFrame ]---- | |
var lastFrame = 0; | |
polyfill('requestAnimationFrame', function(callback) { | |
setTimeout(callback, 16); | |
return lastFrame++; | |
}); | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good catch, I'll just underscore it.