Skip to content

Instantly share code, notes, and snippets.

@wcoder
Last active August 29, 2015 14:23
Show Gist options
  • Save wcoder/fcee724b4f5b44062754 to your computer and use it in GitHub Desktop.
Save wcoder/fcee724b4f5b44062754 to your computer and use it in GitHub Desktop.
Simple timer with JS
/**
* @param seconds - time
* @param tick - callback
* @param result - callback
*/
function timer (seconds, tick, result) {
if (seconds > 0) {
tick(seconds);
seconds -= 1;
setTimeout(function () {
timer(seconds, tick, result);
}, 1000);
} else {
result();
}
}
@wcoder
Copy link
Author

wcoder commented Jun 14, 2015

Example for using

timer(15, function (s) {
    console.log(s + ' second has passed!');
}, function () {
    console.log('Time is over!');
});

Read post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment