Pregunta

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)
¿Fue útil?

Solución

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 ;)

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top