Interruptable timer
Creates a timer which can be paused then resumed per http://stackoverflow.com/questions/3969475
by Hans PUFAL
HTML
<h1>Interruptable timer</h1>
<p>Creates a timer which can be paused then resumed.</p>
<p>Per <a href="http://stackoverflow.com/questions/3969475">Stackoverflow question</a></p>
<h2>Mode d'emploi</h2>
<p>Sets a 5 second timer, after 3 seconds pauses and issues a confirm call. When the
user responds to the confirm resumes the timer (should delay 2 seconds)</p>
JavaScript
function Timer (callback, delay) {
var timerId,
start = new Date ();
this.pause = function () {
window.clearTimeout (timerId);
delay -= new Date () - start;
};
(this.resume = function () {
timerId = window.setTimeout (callback, delay);
}) ();
}
var timer = new Timer (function () { alert ("Done!"); }, 5000);
setTimeout (function () {
timer.pause ();
confirm ('Restart');
timer.resume ();
}, 3000);