Created
May 8, 2013 19:45
-
-
Save hugodias/5543096 to your computer and use it in GitHub Desktop.
Set Interval with pause and resume
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
function RecurringTimer(callback, delay) { | |
var timerId, start, remaining = delay; | |
this.pause = function() { | |
window.clearTimeout(timerId); | |
remaining -= new Date() - start; | |
}; | |
var resume = function() { | |
start = new Date(); | |
timerId = window.setTimeout(function() { | |
remaining = delay; | |
resume(); | |
callback(); | |
}, remaining); | |
}; | |
this.resume = resume; | |
this.resume(); | |
} | |
var timer = new RecurringTimer(function() { | |
document.getElementById("counter").innerHTML = ++count; | |
}, 1000); | |
var count = 0; | |
</script> | |
<input type="button" value="pause" onclick="timer.pause()"> | |
<input type="button" value="resume" onclick="timer.resume()"> | |
<div>Count: <b id="counter">0</b></div> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment