Forum Moderators: open
function timeoutExample(){
return this;
}
timeoutExample.prototype={
f:function(){
alert("Yahar!")
var t=setTimeout("this.f()",50);
}
}
var foo=new timeoutExample();
foo.f();
Thanks in advance
<script type="text/javascript">
function timeoutExample(){
return this;
}
timeoutExample.prototype = {
f : function(te) {
alert("Yahar!");
setTimeout(function(){te.f(te);}, 50);
}
}
var foo=new timeoutExample();
foo.f(foo);
</script>
Note, instead of passing a string as the first parameter to setTimeout, it's better to pass a function reference. In this case, we're passing an anonymous function which calls the f method of the instance of timeoutExample. Scoping problem fixed. :-)
Hope that helps.