I need another help from you guys. Now I'm having trouble using setTimeout function inside the class. From my original example, this is what I did
HTML Code:
function aClassName(param1, param2)
{
//privileged function
this.doSomething = function()
{
alert("I'm doing something...") ;
}
this.run = function()
{
setTimeout("this.doSomething()", 500) ;
}
//constructor begin
this.var1 = param1 ;
this.var2 = param2 ;
this.doSomething() ;
//constructor end
}
So, after creating a new object of this class if I execute the run method like this:
HTML Code:
var myObj = new aClassName(1, 2) ;
myObj.run() ;
it doesn't work. After a lot of googling, it looks like "this" inside setTimeout doesn't refer to the actual object. It refers to the Window. From some of the suggestions, I tried the following:
HTML Code:
function aClassName(param1, param2)
{
//privileged function
this.doSomething = function()
{
alert("I'm doing something...") ;
}
this.run = function()
{
setTimeout("self.doSomething()", 500) ;
}
//constructor begin
this.var1 = param1 ;
this.var2 = param2 ;
var self = this ;
this.doSomething() ;
//constructor end
}
But that didn't work either. Then I used closure and changed the "run" method like this:
HTML Code:
this.run = function()
{
setTimeout(function() {this.doSomething() ; }, 500) ;
}
Is there any other solution to this problem? Thanks again.
Bookmarks