0

Preety straight forward question, though I can't find the answer anywhere I tried these two ways:

setInterval(function(){object/*or this*/.method()},500)

and

setInterval('object/*or this*/.method()',500)
raej99
  • 85
  • 5
  • Your first sample will work fine, but you forgot to invoke method with `()`. – Graham Dec 01 '13 at 22:59
  • What you mean under *"put a method as the parameter"*, maybe you should read this at first [What's the difference between an argument and a parameter?](http://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter) and then [Window.setInterval MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window.setInterval) – Givi Dec 01 '13 at 23:04

2 Answers2

1

setInterval in fact expects a method as the first argument, though there is an alternative syntax where the first argument can be a string of code (not recommended by most)

If you're having issues with that code, it may have to do with the scope of 'this'

setInterval(function(){this.method()},500)

In the above code, 'this' will refer to the closure itself, and wouldn't be the same as 'this.method' occurring outside of that closure. For example, the following would work:

function MyClass() {
    this.thingy = 'yep this is a thingy'
}
var myClass = new MyClass()

// Will log 'MyClass yep this is a thingy'
setInterval(function() { console.log('MyClass', myClass.thingy) }, 1000)

Whereas the following will not work (presuming instantiating the object and calling foo()):

function MyOtherClass() {
    this.thingy = 'also a thingy'
}

// Will log 'MyOtherClass undefined'
MyOtherClass.prototype.foo = function() {
    setInterval(function() { console.log('MyOtherClass', this.thingy) }, 1000)
}

The second example will work if we get around using 'this' within the closure (presuming instantiating the object and calling bar()):

MyOtherClass.prototype.bar = function() {
    var that = this
    setInterval(function() { console.log('MyOtherClass', that.thingy) }, 1000)
}

Also be sure that setInterval is being passed the name of a function:

setInterval(someFunction, 500)

rather than executing a function as an argument

setInterval(someFunction(), 500)

This last line of code is usually a mistake, unless someFunction() returns a function itself ;)

timetocode
  • 71
  • 3
0

The difference between your 2 ways for passing a function to setInterval is whether you want to pass your function as refrence of just copy of it. Allow me to explain it by example:

-1 Referring(demo):

var obj = {
    testMethod: function () {
        console.log('function (testMethod): intial output');
    }
}
setInterval(function () {
    obj.testMethod()
}, 1000);
obj.testMethod = function () {
    console.log('function (testMethod): changed output');
}

when you run this code, the result 'll be execution of the modified version of testMethod. Because here you dont copy the function! Instead, you refer to it. So whenever function implementation is changed, the last modified version is executed.

-2 Copying(demo):

var obj = {
    testMethod: function () {
        console.log('function (testMethod): intial output');
    }
}
setInterval(obj.testMethod, 1000);
obj.testMethod = function () {
    console.log('function (testMethod): changed output');
}

Here all you do is you are passing a copy of the last defined version of the function testMethod to setInterval. So whatever changes you do to testMethod, the result of setInterval will not be changed.

Community
  • 1
  • 1
ebram khalil
  • 8,252
  • 7
  • 42
  • 60
  • I believe that second way is passing function by reference and first it's just executing function as method of `obj` Object. – Givi Dec 02 '13 at 07:56
  • if you can prove it by examples then you right .. check out [this Article](http://www.quirksmode.org/js/this.html) – ebram khalil Dec 02 '13 at 10:04
  • In javascript functions are object, so you copy reference value from `obj.testMethod` and not a function itself. `obj.testMethod` contained reference value from first function before you override it with new anonymous function expression. Look at [***jsFiddle***](http://jsfiddle.net/GKDev/4V5Pu/) and read artciles : [References and Values](http://yoda.arachsys.com/csharp/references.html), [Reference type](https://en.wikipedia.org/wiki/Reference_type), [Value type](https://en.wikipedia.org/wiki/Value_type) – Givi Dec 02 '13 at 10:56
  • And quirks is out of date so would be better if read artciles from [**MDN**](https://developer.mozilla.org/en-US/) and another article about Rvalues and Lvalues [Value](https://en.wikipedia.org/wiki/Value_(computer_science)) – Givi Dec 02 '13 at 11:00