Last active
February 10, 2018 18:00
-
-
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.
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
/* | |
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