Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stop a loop 1

Status
Not open for further replies.

ClulessChris

IS-IT--Management
Jan 27, 2003
890
GB
Hi, I'm still very new new to javascript, but I'm trying.

I wrote the following to be called from a rollover event:
Code:
function roll()
   {
	var p
	var a = 1

	while (a <= 16 )
   		{
		a++
		p = a + ".jpg"
   		document["sub_but"].src = p;
		if (a>=16)
			{
			a=1
			}
   		}
   }

but this is non terminating. How could I amend this to stop running onmouseout?

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Get rid of this:
Code:
if (a>=16)
 {
 a=1
 }

Did you realize that your code changes pictures named 2.jpg through 16.jpg? And it changes ONE image to 15 different images consecutively? Doesn't make much sense to me.

Lee
 
You should use an interval instead.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

var i = false;
var a = 1;

function startAnimation() {
   if (!i) {
      i = window.setInterval("changeImage()", 100);
   }
}

function stopAnimation() {
   if (i) {
      window.clearInterval(i);
      i = false;
   }
   //you might want to put code here to change the
   //element's background back to it's initial state
   //document["sub_but"].src = "initialImage.jpg";
}

function changeImage() {
   document["sub_but"].src = a + ".jpg";
   if (++a >= 16) {
      a = 1;
   }
}


</script>

<style type="text/css"></style>

</head>
<body>

<div onmouseover="startAnimation()" onmouseout="stopAnimation()">blahblahblahblah</div>

</body>
</html>

On a side note, you should use sprites and change the background position instead of loading a newer image. This will result in MANY less server requests.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top