Q. Can we stop an interval using clearInterval, but by using functionName instead intervalID

Lets assume a function:

function abc() {
      console.log("Hello World");

setInterval(abc, 1000);

Can we stop it using: clearInterval(abc);


Or do we always neet to assign an Id to the function and use that Id to clearInterval(IdName)??

But what if the same function has more than one interval? How would it know which one to stop?

You need to store the id.

I wonder if you could store it on the function itself, like:

const abc.intervalID = setInterval(abc, 1000);

// ...


In JS, functions are just objects and can have properties. But putting properties on functions should probably be avoided since it is nonstandard and could cause confusion. Really, you just need to store the interval id somewhere.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.