Skip to content

Instantly share code, notes, and snippets.

@nubunto
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save nubunto/9822547b8d2196d6b9c7 to your computer and use it in GitHub Desktop.

Select an option

Save nubunto/9822547b8d2196d6b9c7 to your computer and use it in GitHub Desktop.
simple and extensible time manipulation
var time = (function() {
var timeout = function(func, t) {
function f() {
// call the function and get the result
var r = func.call(null);
// should we recurse?
if(r) setTimeout(f, t);
}
// schedule the first call
setTimeout(f, t);
}
return {
// how to implement a countdown from start and what to do when it ends.
// pretty useful.
countdown: function(start, fn) {
timeout(function() {
fn.call(null, start--);
return start >= 0;
}, 1000);
},
timeout: timeout
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment