How to use setInterval and clearInterval in NodeJS? [duplicate]

If you want a JavaScript like interval ID after creating a setInterval timer, you can do the following,

function callbackFunc() {
  console.log("I'm just an example callback function");
}

const timeoutObj = setInterval(callbackFunc, 1000);
const intervalId = timeoutObj[Symbol.toPrimitive](); //intervalId is an interger

// Later you can clear the timer by calling clearInterval with the intervalId like,
clearInterval(intervalId);

Note: This only works when your node version is >= v12.19.0.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)