Created
July 2, 2012 16:30
-
-
Save pmcelhaney/3034111 to your computer and use it in GitHub Desktop.
requestAnimationInterval
This file contains hidden or 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
/* | |
requestAnimationInterval() | |
A sort of replacement for setInterval that uses requestAnimationFrame. | |
var myFunction = function (timestamp) { | |
// do some stuff | |
} | |
var interval = requestAnimationInterval(myFunction, 1000); | |
... | |
interval.stop(); | |
... | |
interval.start(); | |
*/ | |
var requestAnimationInterval = function (callback, interval) { | |
var running = true; | |
var tick = function () { | |
requestAnimationFrame(function (timestamp) { | |
callback(timestamp); | |
if (running) setTimeout(tick, interval); | |
}); | |
}; | |
tick(); | |
return { | |
stop: function () { | |
running = false; | |
}, | |
start: function () { | |
if (!running) { | |
running = true; | |
tick(); | |
} | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment