-2

I wanted to clearInterval() and I can do so using clearInterval(myInterval) but why can I not using clearInterval(this) ?

Here is the code that works :

var test =  setInterval(function(){
            request.post({url: myURL, form: {
                user : myUser,
                pass : myPass
                function(err,res,body){
                    if(res.statusCode === 302) clearInterval(test);
                })
        }, 1100)

Here is the code that doesn't work :

setInterval(function(){
            var that = this;
            request.post({url: myURL, form: {
                user : myUser,
                pass : myPass
                function(err,res,body){
                    if(res.statusCode === 302) clearInterval(that);
                })
        }, 1100)

Edit 1 : I am sorry for the poor question. I am not much familiar with the concept of 'this' and intuitively thought using 'this' I can clearInterval(). The reason for that is because when I console.log(test) in the first code and in console.log(this) in the second code inside the setInterval function, the output was the same, hence the intuition. Well, I should rather study about 'this'. Thank you every one for your answers and comments. Much appreciated.

  • 2
    i mean... `this` isn't the same as what a call to setInterval returns. isn't supposed to be. https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work – Kevin B Apr 27 '18 at 17:13
  • setInterval callback is not called with context, the context is window object or undefined if used in strict mode. – jcubic Apr 27 '18 at 17:13
  • setInterval is very old. It doesn't return a reference to it's self, it returns an autoincrementing integer. Which means you can clear intervals with a `for (let i = 0; i < 1e10; i++) { clearInterval(i); }`. – generalhenry Apr 27 '18 at 17:15
  • 2
    @generalhenry: It returns a number on browsers, but not on Node. On Node it returns an object. (But you're still supposed to use the return value to clear it.) – T.J. Crowder Apr 27 '18 at 17:19

2 Answers2

3

setInterval() does not provide the timerID in the value of this. You just can't use it that way. The timerID is only provided as the return value from setInterval() as in your first example.

You could create your own small timer object that encapsulated things however you want, storing the timerID for you.

For example, you could create your own timer object like this that does pass the timer object as the value of this to the callback. Then you could use this to call a clearInterval() method on the object.

class IntervalTimer() {
    start(cb, t) {
        // if there was a previous interval going here, stop it
        // only one per timer object permitted
        this.stop();
        this.id = setInterval(() => {
            cb.call(this);   // set the this value to be our object
        }, t);
    }
    stop() {
        if (this.id) {
            clearInterval(this.id);
            this.id = null;
        }
    }
}

// usage
let t = new IntervalTimer();
t.start(function() {
   // based on some logic, decide to clear the interval
   // the value of "this" has been set to the timer object
   this.stop();
}, 1000);
jfriend00
  • 580,699
  • 78
  • 809
  • 825
-1

In question, in setInterval callback function "this" means context. In the function which is first you write not the same context as timer callback function. Context matters, so using clearInterval must pass the parameter as integer not the context itself.