I have a function with parameters that do some operations on these parameters then send them to itself again with certain timeout Now i want to clear this timeout but at some point i want to recall that timeout that was cleared with its current parameters anyway to do this ?
var thread
$(document).ready(function(){
f(0, 0)
$('.pause').click(function(){
clearTimeout(thread);
});
$('.resume').click(function(){
thread; //want to rerun thread
});
f(x, y) {
thread = setTimeout(function(){
f(x + 1, y + 1);
}, 1000)
}
You just need to call
f(0, 0)
again in theresume
click handler:If you want to continue on from where the
x
andy
values were paused, you need to store them first. Try this:Working example
Note that because the
publicX
andpublicY
variables are now within the scope of all handlers you no longer need to include them as arguments of thef()
function. I've left them in in this example in case you have some other logic which requires their use.