0

Ok I'm javascript newbie and I have to make script for displaying random images after click on dice picture. The problem is that the script shows only only 1 picture after the clcik and it won't continue with the next random images.

<html>
<head>
<script type="text/javascript">
function randomize(){
var delay = 1500;

var imgs = new Array( );
imgs[0] = "0.jpg";
imgs[1] = "00.jpg";
imgs[2] = "01.jpg";
imgs[3] = "02.jpg";
imgs[4] = "03.jpg";
imgs[5] = "04.jpg";

var preload = new Array();
for(n=0; n<imgs.length; n++)
{ preload[n]=new Image();
  preload[n].src=imgs[n];
}

var randomg = Math.round( Math.random( ) * 6 );
if(randomg>=0 && randomg<6){
document.write( "<img alt=\"\" src=\"" + 
imgs[randomg] + "\">" );
alert(randomg);}
else{
document.write( "<img alt=\"\" src=\"" +
imgs[1] + "\">" );}
setInterval("randomize()",delay)
}

</script>
</head>
<body>
<img src="dice.jpg" width="200" height="200" onclick="randomize()">
</body>
</html>
3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by peter_budo
0

try this i also prepared this for my application ..

<script language="javascript">



var delay=1000 //set delay in miliseconds
var curindex=0

var randomimages=new Array()

	randomimages[0]="image1.jpg"
	randomimages[1]="image2.jpg"
	randomimages[2]="image3.jpg"
	randomimages[3]="image4.jpg"
	randomimages[4]="image5.jpg"
	randomimages[5]="image6.jpg"

var preload=new Array()

for (n=0;n<randomimages.length;n++)
{
	preload[n]=new Image()
	preload[n].src=randomimages[n]
}

document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'">')

function rotateimage()
{

if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length)))){
curindex=curindex==0? 1 : curindex-1
}
else
curindex=tempindex

	document.images.defaultimage.src=randomimages[curindex]
}

setInterval("rotateimage()",delay)

</script>

Edited by musthafa.aj: n/a

This question has already been answered. 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.