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 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need help adding a timer to my script.

    First, here's a link to the page that I"m having trouble with: http://www.quantumglobalaero.com/test.php

    On the page you'll see a box that reads Current Specials. I'm trying to slideshow the text inside there. In the code there are separate DIVs for each special. The script works manually (click in the area of the DIV) to cycle through each special. I've been trying to modify Line 60: divs[i].onclick=nextSpecial; to call my function setTimer() but have had no luck. I've also tried to use the setInterval() function without success.

    Here are some of the variations of line 60 I've tried:

    Code:
    divs[i].setTimer=nextSpecial;
    divs[i].setInterval("nextSpecial()" , 5000);
    divs[i].onload=setInterval("nextSpecial();" , 5000);
    Only the first variation throws an error. The others appear to not error but the specials never cycle through. I can't seem to tie it all together. Any suggestions?

    Thank you,
    Mike

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Mdonahue37 View Post
    First, here's a link to the page that I"m having trouble with: http://www.quantumglobalaero.com/test.php
    Code:
    setInterval("nextSpecial()", 5000);
    or if you prefer to start it onload, you can make further use of addLoadEvent:
    Code:
    addLoadEvent( function(){setInterval("nextSpecial()", 5000)} );

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Arty, thanks so much for the quick response. The second option worked like a charm. For my education (and maybe for some others), where would I have placed the first option if I wanted to go that route?

    How do I go about nominating you for help?

    Again, thank you.
    Mike
    Last edited by Mdonahue37; 02-27-2007 at 12:27 AM.

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You've got the right idea with using setInterval() and your setTimer()... The problem is that you're trying to assign it to the div -- setInterval() / setTimeout() are functions of the window.
    The best way would be to set it up in your window.onload (or in your particular case, using your addLoadEvent() function):
    Code:
    var intervalLength=5000; // Length of time between special flips, in ms
    
    function setTimer()
    {
        setInterval("nextSpecial()", intervalLength);
    }
    
    addLoadEvent(prepSpecials);
    addLoadEvent(setTimer);
    Note that I added the setTimer second only to ensure that the specials[] is set before the interval actually runs... However, since your intervals are so long, I doubt that you'd actually have a problem doing it the other way (like you had it)

    I also thought of something else to be aware of... doing the setInterval like I have above will have the div cycle every 5 seconds regardless of whether the user clicked on the div! (note that clicking the div will also cycle the special). Thus in this first scenario, a user may click on the div after reading special[0] for 4 seconds. When the user clicks the div, special[1] is shown, but for only 1 second since the setInterval() will trigger nextSpecial() after 5 seconds have elapsed after special[0] was posted. What you may want to do instead is have it cycle to the next special 5 seconds after the previous special is shown, regardless of how it got there. To do this, you'll need to use setTimeout instead of setInterval, store the "timeout" in a variable, and then clear and restart the next timeout inside nextSpecial():
    Code:
    var intervalLength=5000; // Length of time between special flips, in ms
    var myTimeout; // Stores the timeouts to cycle thru the specials
    
    function setTimer()
    {
        myTimeout = setTimeout("nextSpecial()", intervalLength);
    }
    
    function nextSpecial(){
            // Stop the current timeout
            clearTimeout(myTimeout);
    
            // Cycle to the next special
    	specials[current].style.visibility="hidden";
    	current++;
    	
    	if (current >= total) current = 0;
    	specials[current].style.visibility="visible";
    
            // Set up the new timeout
            myTimeout = setTimeout("nextSpecial()", intervalLength);
    }
    
    addLoadEvent(prepSpecials);
    addLoadEvent(setTimer);
    You may want to check out this tutorial...
    Last edited by Pyth007; 02-27-2007 at 12:35 AM. Reason: Added tutorial site
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #5
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Mdonahue37 View Post
    Arty, thanks so much for the quick response. The second option worked like a charm. For my education (and maybe for some others), where would I have placed the first option if I wanted to go that route?
    Ideally anywhere after the function that it calls and the elements that it references.


  •  

    Posting Permissions

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