Adam j
Adam j

Reputation: 411

Passing setInterval and callback to a new variable

I'm storing setInterval function in an array and trying to pass it to a new variable later on.

this.clock = setInterval(() => {
  this.duration = this.timer.formatTime(this.userTime)
  if (this.duration === '00:00:00') {
    clearInterval(this.clock); 
    this.timer.reset();
  }
}, 1)

Looking in the setInterval object this.clock.callback has a Closure._this object but I don't know how to access it since it is with the <function scope> field.

Is there a way to pass the current contents of the setInterval function to a new variable?

As in...

this.newClock = this.clock

and this.newClock will have the same countdown time as this.clock.

P.S. I'm new to posting on stackoverflow. Any tips or recommendations for asking better questions would be greatly appreciated.

Upvotes: 0

Views: 106

Answers (1)

IMTheNachoMan
IMTheNachoMan

Reputation: 5839

setInterval returns the interval ID for that interval. You cannot use it to get access function. What you could do is create a named function and tell setInterval to call that. And then you can call that function outside of the setInterval too.

function blah()
{
}

setInterval(blah, 1);

blah();

Upvotes: 2

Related Questions