Skip to content

Instantly share code, notes, and snippets.

@pete-otaqui
Last active February 10, 2018 18:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pete-otaqui/5335082 to your computer and use it in GitHub Desktop.
Save pete-otaqui/5335082 to your computer and use it in GitHub Desktop.
A better setTimeout, setInterval and requestAnimationFrame, wrapped as an AMD module and fine for many JSHint setups.
/*
See http://paulirish.com/2011/requestanimationframe-for-smart-animating/
and http://blog.joelambert.co.uk/2011/06/01/a-better-settimeoutsetinterval/
*/
define([
],
function () {
'use strict';
var happytime = {};
happytime.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
var requestAnimFrame = happytime.requestAnimFrame;
happytime.requestTimeout = function(fn, delay) {
if (!window.requestAnimationFrame &&
!window.webkitRequestAnimationFrame &&
!window.mozRequestAnimationFrame &&
!window.oRequestAnimationFrame &&
!window.msRequestAnimationFrame) {
return window.setTimeout(fn, delay);
}
var start = new Date().getTime(),
handle = {};
function loop(){
var current = new Date().getTime(),
delta = current - start;
if (delta >= delay) {
fn.call();
} else {
handle.value = requestAnimFrame(loop);
}
}
handle.value = requestAnimFrame(loop);
return handle;
};
happytime.clearRequestTimeout = function(handle) {
if (window.cancelAnimationFrame) {
return window.cancelAnimationFrame(handle.value);
}
if (window.webkitCancelRequestAnimationFrame) {
return window.webkitCancelRequestAnimationFrame(handle.value);
}
if (window.mozCancelRequestAnimationFrame) {
return window.mozCancelRequestAnimationFrame(handle.value);
}
if (window.oCancelRequestAnimationFrame) {
return window.oCancelRequestAnimationFrame(handle.value);
}
if (window.msCancelRequestAnimationFrame) {
return window.msCancelRequestAnimationFrame(handle.value);
}
return clearTimeout(handle);
};
happytime.requestInterval = function(fn, delay) {
if (!window.requestAnimationFrame &&
!window.webkitRequestAnimationFrame &&
!window.mozRequestAnimationFrame &&
!window.oRequestAnimationFrame &&
!window.msRequestAnimationFrame) {
return window.setInterval(fn, delay);
}
var start = new Date().getTime(),
handle = {};
function loop() {
var current = new Date().getTime(),
delta = current - start;
if(delta >= delay) {
fn.call();
start = new Date().getTime();
}
handle.value = requestAnimFrame(loop);
}
handle.value = requestAnimFrame(loop);
return handle;
};
happytime.clearRequestInterval = function(handle) {
if (window.cancelAnimationFrame) {
return window.cancelAnimationFrame(handle.value);
}
if (window.webkitCancelRequestAnimationFrame) {
return window.webkitCancelRequestAnimationFrame(handle.value);
}
if (window.mozCancelRequestAnimationFrame) {
return window.mozCancelRequestAnimationFrame(handle.value);
}
if (window.oCancelRequestAnimationFrame) {
return window.oCancelRequestAnimationFrame(handle.value);
}
if (window.msCancelRequestAnimationFrame) {
return window.msCancelRequestAnimationFrame(handle.value);
}
return clearInterval(handle);
};
return happytime;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment