...

View Full Version : Need help adding a timer to my script.



Mdonahue37
02-26-2007, 10:50 PM
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:


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

Arty Effem
02-27-2007, 12:03 AM
First, here's a link to the page that I"m having trouble with: http://www.quantumglobalaero.com/test.php

setInterval("nextSpecial()", 5000); or if you prefer to start it onload, you can make further use of addLoadEvent:
addLoadEvent( function(){setInterval("nextSpecial()", 5000)} );

Mdonahue37
02-27-2007, 12:24 AM
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

Pyth007
02-27-2007, 12:29 AM
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):


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():


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 (http://www.webmonkey.com/webmonkey/98/29/index2a.html?tw=programming)...

Arty Effem
02-27-2007, 12:59 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum