Skip to content Skip to sidebar Skip to footer

Reset Timer Of SetInterval()

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

Solution 1:

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);

Solution 2:

you need to add a function that clears the interval and then restarts it

function resetInterval() {
  clearInterval(timer);

  timer = setInterval(function() {
    console.log("restarted interval");
    test1();
   }, 5000); 
 }

then you can simply call it as needed

chat.client.addMessage = function(data) {
  resetInterval();
  test2(data);
};

Post a Comment for "Reset Timer Of SetInterval()"