...

View Full Version : Recursion, Objects, and setTimeout(). Oh my!



Skyzyx
04-12-2003, 01:06 AM
I'm re-writing one of my message ticker scripts as an object. The problem is that once a variable is initialized as a new instance of my object, it can't seem to reference an internal function (this.functionName()) without causing an error.

Here's a snippet:


function messageCenter(spanId, delayInMS)
{

. . .

// Start Messages.
this.start=function(startWhere)
{
current=(startWhere) ? startWhere:0;

document.getElementById(this.spanId).innerHTML=mcMessages[current];

if (current == mcMessages.length-1) current=0;
else current++;

// The bug is with the line below
var tOut=setTimeout("this.start()", this.delay);
}

. . .

}

// Create a new instance of the object.
var msg=new messageCenter('show');

// Add the messages. This works.
msg.addMessage('What do you want?');
msg.addMessage('Hmmm...');
msg.addMessage('All your mother are belong to us.');

//-->
</script>
</head>

<!-- Load the ticker after body has loaded -->
<body onload="msg.start();">


It doesn't seem to want to recursively call itself, since this.start() is now msg.start().

I've included the entire script here. I believe it all works, other than this recursive part.

joh6nn
04-12-2003, 09:11 AM
when setTimout() evalutes the string that you pass it, it does so at a global, not local, level. so when this == window

beetle
04-12-2003, 07:41 PM
Use Algorithm's Timer class (http://www.codingforums.com/showthread.php?s=&threadid=10531). It works great, as I've implemented it in my own news ticker (http://www.peterbailey.net/test/ticker.htm) and several other classes (http://www.peterbailey.net/dev/jsclasses)

cheesebagpipe
04-12-2003, 11:45 PM
See if this helps...

http://www.crockford.com/javascript/private.html



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum