0

Hello everyone! I need help with my code, I want to alternate more than just 2 images using a function and setInterval. Here is my code, can anyone tell me what's wrong with it?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Banner Advertising</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<style type="text/css">
body { font-family: "Trebuchet MS" }
</style>

<script type="text/javascript">
/* <![CDATA[ */

var intImage = 2;
function concerts(){
	switch (intImage){
		case 1:
			document.images[0].src = "concert1.gif";
			intImage = 2;
			break;
		case 2:
			document.images[0].src = "concert2.gif";
			intImage = 1;
			break;
		case 3:
			document.image[0].src = "concert3.gif";
			intImage = 4;
			break;
		case 4:
			document.image[0].src = "concert4.gif";
			intImage = 5;
			break;

		case 5:
			document.image[0].src = "concert5.gif";
			intImage = 1;
			break;		
		}
}

/* ]]> */
</script>
</head>

<body onload="var begin=setInterval('concerts()',5000);">
<p><img src="concert1.gif" alt="Changing concert advert images" /></p>

</body>
</html>

I also tried using if... else but I'm not cracking it. :?:

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Taywin
0

Those images won't work by changing only image URL source because they are not loaded onto the page. JavaScript is not Ajax that sends request to the server to return the image. You need to load all images and hide those you don't want to be displayed. Then use Javascript to set/change the style in order to display what you want. You can look at http://www.daniweb.com/forums/thread272711.html as an example. There are many ways to do it too.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.