Skip to content

Instantly share code, notes, and snippets.

@varemenos
Created June 7, 2013 16:53
Show Gist options
  • Save varemenos/5730688 to your computer and use it in GitHub Desktop.
Save varemenos/5730688 to your computer and use it in GitHub Desktop.
JavaScript - Timer functionality
// from: http://stackoverflow.com/a/3969760/649239
function Timer(callback, delay) {
var timerId, start, remaining = delay;
this.pause = function() {
window.clearTimeout(timerId);
remaining -= new Date() - start;
};
this.resume = function() {
start = new Date();
timerId = window.setTimeout(callback, remaining);
};
this.resume();
}
var timer = new Timer(function() {
alert("Done!");
}, 1000);
timer.pause();
// Do some stuff...
timer.resume();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment