gangelo
August 18th, 2002, 01:45 PM
Does anyone know a way to invoke an object method as the first param to setTimeout()? Obviously, the "this" pointer for my class object is not available when setTimeout() tries to execute the code, but it shows what I want to ultimately do.
Any help would be appreciated :)
// In a <script> tag in <head>...
function CClass()
{
this.id = null;
this.counter = 0;
this.milliseconds = 5000;
CClass.prototype.startMe = function()
{
this.id = setTimeout("this.showMe('hello world!')", this.milliseconds);
}
CClass.prototype.showMe = function(string)
{
alert(string + ": " + this.counter);
if(++this.counter == 3)
{
clearTimeout(this.id); // simple test, avoid endless loop
return;
}
// Start us again...
this.id = setTimeout("this.showMe('hello world!')", this.milliseconds);
}
}
var myClass = new CClass();
<!-- In html... -->
<body onLoad="JavaScript:myClass.startMe();"> ...
Any help would be appreciated :)
// In a <script> tag in <head>...
function CClass()
{
this.id = null;
this.counter = 0;
this.milliseconds = 5000;
CClass.prototype.startMe = function()
{
this.id = setTimeout("this.showMe('hello world!')", this.milliseconds);
}
CClass.prototype.showMe = function(string)
{
alert(string + ": " + this.counter);
if(++this.counter == 3)
{
clearTimeout(this.id); // simple test, avoid endless loop
return;
}
// Start us again...
this.id = setTimeout("this.showMe('hello world!')", this.milliseconds);
}
}
var myClass = new CClass();
<!-- In html... -->
<body onLoad="JavaScript:myClass.startMe();"> ...