We can't get the value returned by setInterval () in javascript, but we still want to get rid of it, so how to get there?

var I = setInterval () this function we call him when we return an I is his id
when this method is encapsulated in a certain block we can not get this I, then do we have a way to clear this timer
if there is no way to clear this setInterval alone, is there a way to clear all the current setInterval timers?

Apr.27,2021

1: when encapsulating, you can use the timer id as part of the return value, and then clear
2 through clearInterval: encapsulate the method that adds the cleared operation


then you encapsulate a clearInterval method to the same place and call it when you want to clear it.


it is recommended to encapsulate a mySetInterval, to facilitate operation

function mySetInterval(func, delay) {
   const id = setInterval(func, delay);
   return () => clearInterval(id);
}
const cancel = mySetInterval(func, delay);
cancel();

add a method to your module to clear the timer, and you can call

outside the module.
function destory(){
    //...Interval
}

function timer() {
  var timeout;
  ...
  var timered = function(){
    timeout = setInterval(function() {
      ...
    })
  }
  timered.cancel = function() {
    clearInterval(timeout)
    timeout = null
  }
  return timered
}

because when the previously written setInterval is cleared with clearInterval, the timeout returned by setInterval will be lost. When I do this, I put the timeout push returned by setInterval into the arr array, and determine whether the arr.length is greater than zero before each re-call of setInterval, and call the clearInterval cleanup timer if it is greater than zero.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3fcf3-2c4a4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3fcf3-2c4a4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?