Click to See Complete Forum and Search --> : Using setTimeout in a OBject method


Paolo
07-11-2003, 04:43 AM
In a object method i've use setTimeout to launch itself but after the first time the object method lost object properties.
(hope someone understand my problem).

what i do is:

function nameObj(param1, param2)
this.prop1=param1;
this.prop2=param2;
this.method1=func1;
}

function1 {
...
setTimeout(this.method1(),200);
}

P.S.: i'm newbie of Object programming

Gollum
07-11-2003, 06:14 AM
We all have to start somewhere :)

The problem you have is that the function window.setTimeout() takes a function, not an object as its first argument, and when you say

window.setTimeout(this.method1(), 200)

you are actually calling method1, getting its return value and sending that to setTimeout.

or did you mean to say

window.setTimeout("this.method1()", 200) or window.setTimeout(this.method1, 200)?

anyway, since the object is lost, all the member state of the object is lost too.

perhaps you could try

window.setTimeout(function(){this.method1();},200);

but this will not be supported on older browsers

Paolo
07-11-2003, 11:55 AM
Thanks for help.

I applied a strategy...

1) i declare a function (ex: pippo) which call the object method
2) i'm setting the function setTimeout to call pippo.

i don't know if its correct but its work.

:rolleyes: