I was making timers and was having trouble clearing them since if you use the same variable to save each timer and you use clearTimeout(timer); it only clears the last timer and not all of them. You can't just call clearTimeout 3 times to clear them. So I found this col code to put setTimeouts into an array and clear them before you set more.
Make many timers and clear them any time.
<script>
timers = new Array();
function resetTimeouts() {
timers = new Array();
}
function clearTimeouts() {
for (var i= 0;i < timers.length; i++) {
clearTimeout(timers[i]);
}
//resetTimeouts();
timers = new Array();
}
//stop any timer
clearTimeouts();
timers.push(setTimeout("alert('timer here');", 2000))
</script>
Then if you make many timers and then you make more you want to clear all the old ones each time you make more so the old ones aren't triggered after new ones are made.
No comments:
Post a Comment