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>

Recommended Answers

All 2 Replies

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>

JSP stands for Java Server Pages not Java Script.

Post moved

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.