Skip to content

Instantly share code, notes, and snippets.

@davidgarsan
Created July 23, 2013 14:12
Show Gist options
  • Save davidgarsan/6062662 to your computer and use it in GitHub Desktop.
Save davidgarsan/6062662 to your computer and use it in GitHub Desktop.
Do something each second using requestAnimationFrame instead of setTimeout or setInterval.
var time = new Date().getTime();
var interval = 1000;
(function update() {
requestAnimationFrame(update);
var now = new Date().getTime(),
delta = now - (time || now);
if(delta>=interval) {
// Update a "time ago"|"time left" or do something...
console.log((new Date(now)).getSeconds());
time = now;
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment