Solution 1 :

You can create a class that starts/stops an interval.

Repl Example

class Interval {
  constructor(duration, callback) {
    this.duration = duration
    this.callback = callback
  }
  get interval() { return this._interval }
  set interval(interval) { this._interval = interval }
  get duration() { return this._duration }
  set duration(duration) { this._duration = duration }
  get callback() { return this._callback }
  set callback(callback) { this._callback = callback }
  start() {
    this.interval = setInterval(this.callback, this.duration)
    return this
  }
  stop() {
    clearInterval(this.interval)
    return this
  }
}

Instantiation

let interval = new Interval(
  1000, 
  () => {
    console.log('interval')
  }
)
interval.start()
setTimeout(() => {
  interval.stop()
}, 5000)

Solution 2 :

You can’t. The interval id “is a numeric, non-zero value which identifies the timer created by the call to setInterval()” and is generated automatically.

I would recommend changing the logic of your code but, if you insist, you could add all the ids in an array, which you would pass as a parameter to the function that erases the intervals. Then you would splice the ids as you kill the intervals.

Problem :

So basically I would like something like this

Inter = setInterval(Function, 1000);
clearInerval(Inter);
Inter = setInterval(Function, 1000);

For some reason something similar won’t work for me, I’d post it all but I have no comments and no sense of organization. Is this supposed to work, or is their something I am missing?

Edit : Okay so I’ll try to better explain this. So basically, I have an interval set, and a function to clear said interval. I need these to reset after every input, so the clearInterval has to recognize an interval with the same ID as it began with, so I need to basically re-use the ID, but for some reason it doesn’t work. Hopefully thast madness made semi sense

Comments

Comment posted by Pointy

setInterval()

Comment posted by Manuel Abascal

What are you trying to accomplish with this? I’m not sure I understand what you’re trying to do…

By