/    Sign up×
Community /Pin to ProfileBookmark

Problem with setTimeout()

Hello,

I’m trying to make a slideshow that automatically plays when the screen is loaded. I also want it to be able to change images by clicking on forward and backward links on the slideshow to change the image. I’ve been able to do one or the other , but when I try to do them both things just dont work right. I think my problem is with the setTimeout() function. heres what ive got

<head>

var image3=new Image()

image3.src=”gallery/3503111448_363c0a2650.jpg” <——-this is just an example of how the images are loaded in. PHP takes care of this, its working fine.

function slideit(){

if (!document.images)
return
document.images.slide.src=eval(“image”+step+”.src”)

}

function stepup() {
if (step<count)
step++
else
step=1;
slideit()
}
function stepdown() {
if (step==1)
step = count
else
step–;
slideit()
}

</head>

<body>
<div id=”displaycase” onmouseover=”showslide()” onmouseout=”hideslide()”>
<img src=”” name=”slide”>
<div style=”height:20px;width:40px;margin-top:-20px;padding-left:5px;visibility:hidden;” id=”slider”>
<a href=”javascript: void(0);” onclick=”stepdown()”><span class=”trans”><strong style=”font-size:24px;”>&lt;</strong></span></a>
<a href=”javascript: void(0);” onclick=”stepup()”><span class=”trans”><strong style=”font-size:24px;”>&gt;</strong></span></a>
</div>
</div>
<script>

var step=1

var count=(taken care of by php)
function slideitr(){

if (!document.images)
return
document.images.slide.src=eval(“image”+step+”.src”)

if (step<3)
step++
else
step=1

setTimeout(“slideitr()”,4000)
}
slideitr()

</script>

I’ve left out the PHP code that populates the images and the count, they are working fine, its the way everything is working together thats the trouble. When I invoke the stepup() it either skips one and plays or does nothing. the stepdown() seems to work, but after a few clicks back it just stops. I tried fixing the stepup() function by taking out the incrementer within the function, thinking that would stop it from skipping , but it makes the button do nothing instead. any incites?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@cryptographerMay 25.2009 — Well, for number one, apparently you're not even wrapping the head section in script tag. Huge no-no.
Copy linkTweet thisAlerts:
@nibbcnobleauthorMay 25.2009 — lol. I actually did wrap it in a script tag just left it out of my post. My page wouldn't have worked at all in that case!, since I posted the specific problem I had (which is now fixed), I thought I made it clear , just left out a few details since they weren't important.

Doesn't matter anymore, I figured out a good way to use the clearTimeout() function and either move the slideshow forward or back and then start the slideshow again. So basically hitting the "forward button" stops the slideshow, repositions, and then starts it again. I had read that you couldn't stop a settimeout() in the middle of operation but that is obviously not true as its working beautifully. Cant believe everything you hear, that is for sure.
×

Success!

Help @nibbcnoble spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...