request-animation-frame
Version:
requestAnimationFrame shim
47 lines (42 loc) • 1.79 kB
JavaScript
(function() {
var max, now, _ref, _ref2;
now = (_ref = Date.now) != null ? _ref : function() {
return new Date().getTime();
};
max = Math.max;
_ref2 = (function() {
var cancel, isNative, last, request, vendor, _i, _len, _ref2;
last = 0;
request = typeof window !== "undefined" && window !== null ? window.requestAnimationFrame : void 0;
cancel = typeof window !== "undefined" && window !== null ? window.cancelAnimationFrame : void 0;
_ref2 = ["webkit", "moz", "o", "ms"];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
vendor = _ref2[_i];
if (cancel == null) {
cancel = (typeof window !== "undefined" && window !== null ? window["" + vendor + "cancelAnimationFrame"] : void 0) || (typeof window !== "undefined" && window !== null ? window["" + vendor + "cancelRequestAnimationFrame"] : void 0);
}
if ((request != null ? request : request = typeof window !== "undefined" && window !== null ? window["" + vendor + "RequestAnimationFrame"] : void 0)) {
break;
}
}
isNative = request != null;
request = request != null ? request : function(callback, timeout) {
var cur, id, time;
if (timeout == null) timeout = 16;
cur = now();
time = max(0, timeout + last - cur);
id = setTimeout(function() {
return typeof callback === "function" ? callback(cur + time) : void 0;
}, time);
last = cur + time;
return id;
};
request.isNative = isNative;
isNative = cancel != null;
cancel = cancel != null ? cancel : function(id) {
return clearTimeout(id);
};
cancel.isNative = isNative;
return [request, cancel];
})(), this.requestAnimationFrame = _ref2[0], this.cancelAnimationFrame = _ref2[1];
}).call(this);