Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Recursion, Objects, and setTimeout(). Oh my!

    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:
    Code:
    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.
    Attached Files Attached Files

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    when setTimout() evalutes the string that you pass it, it does so at a global, not local, level. so when this == window
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use Algorithm's Timer class. It works great, as I've implemented it in my own news ticker and several other classes
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •