/    Sign up×
Community /Pin to ProfileBookmark

setTimeout prevents resetting of image

I am using setTimeout to provide a delay between changing images. The images are changed by looping back through a function.
clearTimeout is used to cancel the setTimeout function.

The problem is that I would also like the initial image (image1.jpg) to be displayed when I’m finished with the looping. I just cannot get this to happen.
The code in RED appears to be ignored. This is the code that would would revert the image back to image1.jpg.

The following code gives an idea of what I’m trying to do. I use a DIV for the button.
Any help would be appreciated.

<html>
<head>
<title>Untitled</title>

<script language=”javascript”>
var buttonStatus;
var currentImage = 1;
var timer;

function Start() {
buttonStatus = 1;
}

function Stop() {
buttonStatus = 0;
EndTimer();
}

function ChangeImage(fileName, imageName, imageNum) {

if (currentImage > imageNum) {
currentImage = 1;
}
image = fileName + currentImage + ‘.jpg’;
document.images(imageName).src = image;
currentImage++;

if (buttonStatus == 1) {
timer = setTimeout(“ChangeImage(‘” + fileName + “‘, ‘” + imageName + “‘, ” + imageNum + “)”, 1000);
}else{
[COLOR=Red]currentImage = 1;
image = fileName + currentImage + ‘.jpg’;
document.images(imageName).src = image;[/COLOR]
}

}

function EndTimer() {
clearTimeout(timer);
}
</script>

</head>
<body>

<div id=”div1″ style=”position: absolute; top: 100px; left: 900px; width: 30px; height: 10px; border: solid;” onMouseOver=”Start()” onClick=”ChangeImage(‘C:/Test/image’, ‘pics’, 5)” onMouseOut=”Stop()”>
</div>

<img src=”image1.jpg” name=”pics”>

</body>
</html>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 25.2005 — Get rid of the references to [b]EndTimer[/b] if you want your code to put the image back to the first one.
×

Success!

Help @TedN 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...