0

This is what I am trying to do and it isn't working, Error it is showing is : this.run is not a function. on the line ( this.xId = window.setInterval( 'this.run()', 2500 ); )

function(){

   this.run = function(){

      DO SOMETHING;

   }

   this.xId = window.setInterval( 'this.run()', 2500 );

}

What could be the reason ?

1
  • Because the string "this.run()" is evaluated in global scope and there, this refers to window. I assume you don't have a function run in global scope. Commented May 24, 2012 at 12:03

2 Answers 2

1

You need to pass anonymously function for that:

this.xId = window.setInterval( function() { this.run() }, 2500 );

Or better is to bind this function with this context:

this.xId = window.setInterval( this.run.bind(this) , 2500 );

Note that bind are implemented in ECMA-262, 5th edition, so for crossbrowser compatibility you need to add this:

if (!Function.prototype.bind) {  
  Function.prototype.bind = function (oThis) {  
    if (typeof this !== "function") {  
      // closest thing possible to the ECMAScript 5 internal IsCallable function  
      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");  
    }  

    var aArgs = Array.prototype.slice.call(arguments, 1),   
        fToBind = this,   
        fNOP = function () {},  
        fBound = function () {  
          return fToBind.apply(this instanceof fNOP  
                                 ? this  
                                 : oThis || window,  
                               aArgs.concat(Array.prototype.slice.call(arguments)));  
        };  

    fNOP.prototype = this.prototype;  
    fBound.prototype = new fNOP();  

    return fBound;  
  };  
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can the function be invoked like this window.setInterval(this.run,2500 ) ?; If not pls explain the difference ?
The bind version worked, thanks. But I am confused about the "ECMA-262, 5th edition", where am I supposed to add it ?
Add this to some js file that is included on top of all pages.
0
  • First of all, use a function reference rather than a string for the timeout callback. the this when using a string is the global object, and not what might have provided it.

  • Secondly, cache the value of this. The this in the callback may not be the same as the one outside it (which is the one attached with run).

    function(){
        var that = this; //cache "this"
    
        this.run = function(){
            //DO SOMETHING;
        }
    
        this.xId = window.setInterval(function(){
            that.run()
        },2500);
    }
    

1 Comment

As an alternative to "caching the value of this" you could use the bind function as described here: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.