I had a given a assignment of java script as
to form a script that changes the background image of the document after equal interval of time.as i am a beginner to programming I had formed a script that should change background and a loop stated for image name to be taken from the file with common extension.In all there are 27 image which should be displayed in serial number.
but not working
here is the code

<html>
<script language="javascript">
function f1()
{
var n;
window.setTimeout("f1()",1200);
for (n=1;n<27;n++)
document.body.background=(n+".png");
}

</script>
<body>
<input type="button" onMouseover="f1()" value="Change Document Color"><p>
</body>
</html>

Recommended Answers

All 5 Replies

Member Avatar for stbuchok

Use setInterval:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">

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

<script>

var timeout = setInterval(function(){
	document.body.background = 'Images/image_' + Math.floor(Math.random() * 9) + '.png';
}, 1200);

</script>

</head>
<body>


</body>
</html>

I had Tried Using this Code but the Images which are displaying are at random

The image has to be in serial

Member Avatar for stbuchok

Is this your homework, if so I want the credit:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">

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

<script>

var i = 0;

var timeout = setInterval(function(){

	if(i === 27){
		i = 0;
	}
	else{
		i++;
	}

	document.body.background = 'Images/image_' + i + '.png';
}, 1200);

</script>

</head>
<body>


</body>
</html>
commented: Thanxx a lot sir for your help +0

Thank you sir Its works Great.
My Teacher will be amazed by this code
and all the credit goes to you

and you explain me what was going wrong with my original code
please

And also please explain me the if function of your code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.