Skip to content

Instantly share code, notes, and snippets.

@KrofDrakula
Created October 1, 2011 12:49
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 KrofDrakula/1256023 to your computer and use it in GitHub Desktop.
Save KrofDrakula/1256023 to your computer and use it in GitHub Desktop.
Polyfills the `requestAnimationFrame` function for all browsers.
(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);
@w0rse
Copy link

w0rse commented Nov 29, 2011

It turns out that 'native' is a reserved keyword in js. This code works ok in browser, but things like jslint and yui compressor are throwing errors about variable with that name.

@KrofDrakula
Copy link
Author

Good catch, I'll just underscore it.

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