How can I clear this timeout:
var timeout = setTimeout(function() {
window.setTimeout(arguments.callee, 1000);
}, 1000)
clearTimeout(timeout)
don't working..
How can I clear this timeout:
var timeout = setTimeout(function() {
window.setTimeout(arguments.callee, 1000);
}, 1000)
clearTimeout(timeout)
don't working..
You're setting timeout
to the result of the first call to setTimeout
, but not any following calls. You probably want
var timeout;
timeout = setTimeout(function() {
timeout = window.setTimeout(arguments.callee, 1000);
}, 10);
clearTimeout(timeout);
But this would be better written as
function iter() {
timeout = setTimeout(iter, 1000);
}
Store the timeout in a variable that is outside of the scope of the recursion. Also avoid using arguments.callee due to IE's limitations. You can check a working fiddle here.
var timeout = null;
function updateTimeout() {
timeout = setTimeout(updateTimeout, 1000)
console.log('hoi')
}
// Start the timeout
updateTimeout();
You need to reassign timeout
each time you set new timeout. And get rid of arguments.callee
.
Try this working example:
<button onclick="myFunction()">Try it</button>
<button onclick="myStopFunction()">Stop the alert</button>
<script>
var timeout;
function myFunction() {
timeout = setTimeout(function timerHandler() {
timeout = setTimeout(timerHandler, 1000);
}, 1000)
}
function myStopFunction() {
clearTimeout(timeout);
}
</script>