PDA

View Full Version : How to stop "setInterval"?



labritz
Nov 29th, 2009, 04:35 PM
Here is what I have. I just can't figure out how to stop it from starting an setInterval over and over when I mouseover. It only supposed to start once and stop when i hit the stop button. Any help? The instructions said "Before starting a setInterval(), you should clear any running interval that may already be running. Otherwise, several versions of the setInterval method may run at the same time." but i just can't figure out how.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-…
<html>
<head><title>animation example</title>
<script type="text/javascript">

var pic1 = new Image();
pic1.src = "light1.gif"

var pic2 = new Image();
pic2.src = "light2.gif"

var pic3 = new Image();
pic3.src = "light3.gif"


function showPics() {

image=document.getElementById("lightPic"…

if(image.src == pic1.src)
image.src = pic2.src;
else if (image.src == pic2.src) {
image.src=pic3.src;
}
else if (image.src == pic3.src) {
image.src=pic1.src;
}
}

</script>

</head>

<body>

<img id="lightPic" src="light1.gif" onMouseOver= "timerID = setInterval('showPics()', 1000)" />

<form>

<input type="button" value="Stop" onclick="clearInterval(timerID)" />

</form>
</body>
</html>

labritz
Nov 29th, 2009, 06:24 PM
anyone?

Philip M
Nov 29th, 2009, 06:31 PM
Like this:-




<script type="text/javascript">
var tim;
//.........................
</script>


<img id="lightPic" src="light1.gif" onmouseover= "window.clearInterval(tim); tim = window.setInterval('showPics()',2000)"/>

<form>
<input type="button" value="Stop" onclick="clearInterval(tim)" />
</form>


BTW, please help us to help you by following the posting guidelines and wrapping your code in [code] tags. This means use the octothorpe or # button on the toolbar -which will insert opening [ code ] and closing [ /code ] tags - omit the spaces. You can (and should) edit your previous post. And please do not expect a reply (and bump) within two hours, especially on a Sunday.


Quizmaster: What M, named after the Hebrew word for "Institution", is the national intelligence agency of Isreal?
Contestant: Al-Qaeda.

labritz
Nov 29th, 2009, 06:34 PM
Like this:-




<script type="text/javascript">
var tim;
//.........................
</script>


<img id="lightPic" src="light1.gif" onmouseover= "window.clearInterval(tim); tim = window.setInterval('showPics()',2000)"/>

<form>
<input type="button" value="Stop" onclick="clearInterval(tim)" />
</form>


BTW, please help us to help you by following the posting guidelines and wrapping your code in [code] tags. This means use the octothorpe or # button on the toolbar -which will insert opening [ code ] and closing [ /code ] tags - omit the spaces. You can (and should) edit your previous post.

Quizmaster: What M, named after the Hebrew word for "Institution", is the national intelligence agency of Isreal?
Contestant: Al-Qaeda.

tim?

window.clearinterval?

EDIT: oh. is see. i didn't need the "window." though. other than that it worked perfectly.

Philip M
Nov 29th, 2009, 07:26 PM
tim?

window.clearinterval?

EDIT: oh. is see. i didn't need the "window." though. other than that it worked perfectly.

The correct syntax is window.setInterval() and window.clearInterval(). Most browsers will forgive this.