polyfill-service
Version:
A polyfill combinator
50 lines (38 loc) • 1.11 kB
JavaScript
(function (global) {
if ('mozRequestAnimationFrame' in global) {
global.requestAnimationFrame = function (callback) {
return mozRequestAnimationFrame(function () {
callback(performance.now());
});
};
global.cancelAnimationFrame = mozCancelAnimationFrame;
} else if ('webkitRequestAnimationFrame' in global) {
global.requestAnimationFrame = function (callback) {
return webkitRequestAnimationFrame(function () {
callback(performance.now());
});
};
global.cancelAnimationFrame = webkitCancelAnimationFrame;
} else {
var lastTime = Date.now();
global.requestAnimationFrame = function (callback) {
if (typeof callback !== 'function') {
throw new TypeError(callback + 'is not a function');
}
var
currentTime = Date.now(),
delay = 16 + lastTime - currentTime;
if (delay < 0) {
delay = 0;
}
lastTime = currentTime;
return setTimeout(function () {
lastTime = Date.now();
callback(performance.now());
}, delay);
};
global.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
}
})(this);