Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reset Timer of setInterval()

 var timer;
 chat.client.addMessage = function (data) {
     clearTimeout(timer);
     test2(data);
 };
 timer = setInterval(function () {
     console.log("working");
     test1();
 }, 5000);  

I am trying to restart timer when ever chat.client.addMessage is executed.SetInterval is executed after every 5000ms until chat.client.addMessage is executed when ever that method is executed setInterval Function stops executing . Help will be appreciated:)

like image 652
user2137186 Avatar asked Aug 16 '13 09:08

user2137186


People also ask

How do I reset my setInterval timer?

To reset the setInterval timer with JavaScript, we can use the clearInterval function. const myFn = () => { console. log("idle"); }; let myTimer = setInterval(myFn, 4000); //... clearInterval(myTimer); myTimer = setInterval(myFn, 4000);

How do I reset setInterval after clearInterval?

click(function () { clearInterval(refreshIntervalId); }) }); It stops the interval timer. The 'setInterval' starts an interval timer that calls the 'update' function every 10 seconds. The 'clearInterval' call stops that timer.

How do you reset intervals in react?

Clearing setInterval in React To stop an interval, you can use the clearInterval() method. ... useEffect(() => { const interval = setInterval(() => { setSeconds(seconds => seconds + 1); }, 1000); return () => clearInterval(interval); }, []); ...

How do I terminate setInterval?

If you set the return value of setInterval to a variable, you can use clearInterval to stop it.


1 Answers

You need to use clearInterval instead of clearTimeout as clearTimeout is the inverse of setTimeout. You can use it in the same manner:

clearInterval(timer);
like image 101
CodingIntrigue Avatar answered Sep 23 '22 06:09

CodingIntrigue