I am setting a timeout ex:
button.onClick(
timer = setTimeout(func(){}, 1000);
);
When the button is clicked again, I want to set timer back to 1000 without it running func().
As is, multiple clicks of the button just make the timer go off at some random time. I've tried calling clearTimeout(timer) before, but it does nothing.
I've run into this problem before several times where I just want to reset the timer on the trigger event, and have always had to end up finding a workaround. I can't imagine there is no way to just put the timer back to maximum. Any insight without writing my own version of setTimeout from scratch is greatly appreciated.
I am setting a timeout ex:
button.onClick(
timer = setTimeout(func(){}, 1000);
);
When the button is clicked again, I want to set timer back to 1000 without it running func().
As is, multiple clicks of the button just make the timer go off at some random time. I've tried calling clearTimeout(timer) before, but it does nothing.
I've run into this problem before several times where I just want to reset the timer on the trigger event, and have always had to end up finding a workaround. I can't imagine there is no way to just put the timer back to maximum. Any insight without writing my own version of setTimeout from scratch is greatly appreciated.
Share Improve this question asked May 3, 2017 at 4:26 verstehe_verstehe_ 981 silver badge7 bronze badges 1- 1 so you want to remove the previous timer and reset its time to 1000ms? – madalinivascu Commented May 3, 2017 at 4:31
2 Answers
Reset to default 5Set the timer as a global variable, clear the previous timer on click
var timer;
button.onClick(
clearTimeout(timer);
timer = setTimeout(func(){}, 1000);
);
button.onClick(
clearTimeout(timer);
timer = setTimeout(func(){}, 1000);
);
See https://developer.mozilla/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout