javascript - Reset setTimeout() back to maximum time without executing? - Stack Overflow

I am setting a timeout ex:button.onClick(timer = setTimeout(func(){}, 1000););When the button is click

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
Add a ment  | 

2 Answers 2

Reset to default 5

Set 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744997170a4605266.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信