Reply to topic
Need Help diagnosing the fault in this randomization loop
jydeda


Joined: 22 Jan 2008
Posts: 1
Location: London England
Reply with quote
Can anyone help with diagnosing why this code which generates random images is not looping the randomization process. It generates two other images related to the one it selects from the list and then should go back to the beginning and select another image from the list and repeat the process Embarassed

Cheers!!

Code:
<html>
<head>
<script type="text/javascript">
var interval = 1.5; // delay between rotating images (in seconds)
var random_display = 1; // 0 = no, 1 = yes
var path_prefix = 'E:/path/leading/to/images/';
interval *= 1000;

var image_index = 0;
var image_list = [
'01BbStv.jpg','02B0Stv.jpg','03C0Stv.jpg','04DbStv.jpg','05D0Stv.jpg',
'06EbStv.jpg','07E0Stv.jpg','08F0Stv.jpg','09GbStv.jpg','10G0Stv.jpg',
'11AbStv.jpg','12A0Stv.jpg','13BbStv.jpg','14B0Stv.jpg','15C0Stv.jpg',
'16DbStv.jpg','17D0Stv.jpg','18EbStv.jpg','19E0Stv.jpg','20F0Stv.jpg',
'21GbStv.jpg','22G0Stv.jpg','23AbStv.jpg','24A0Stv.jpg','25BbStv.jpg',
'26B0Stv.jpg','27C0Stv.jpg','28DbStv.jpg','29D0Stv.jpg','30EbStv.jpg',
'31E0Stv.jpg','32F0Stv.jpg','33GbStv.jpg','34G0Stv.jpg','35AbStv.jpg',
'36A0Stv.jpg','37BbStv.jpg','38B0Stv.jpg','39C0Stv.jpg','40DbStv.jpg',
'41D0Stv.jpg','42EbStv.jpg','43E0Stv.jpg','44F0Stv.jpg','45GbStv.jpg',
'46G0Stv.jpg','47AbStv.jpg'];

var number_of_images = image_list.length;

function generate(x, y) {
   var range = y - x + 1;
   return Math.floor(Math.random() * range) + x;
}
function getNextImage() {
   image_index = random_display ? generate(0, number_of_images) : (++image_index) % number_of_images;
   return image_list[image_index];
}
function rotateImage() {
var new_image = getNextImage();
//Image one
var img1 = function () {
document.getElementById('sImage').src = path_prefix + new_image;
//Go to the second part of the loop
setTimeout(img2, interval);
}
//Image two
var img2 = function () {
document.getElementById('kImage').src = path_prefix + new_image.replace(/.{3}\./, 'key.');
//Go to the third part of the loop
setTimeout(img3, interval);
}
//Image three
var img3 = function () {
document.getElementById('fImage').src = path_prefix + new_image.replace(/.{3}\./, 'Fbd.');
}
//Re-Start the loop
setTimeout(img1, interval);
}
window.onload = function() {
   setTimeout(rotateImage, interval);
}
</script>
</head>

<body>
   <img id="sImage" src="" width="33%" height="100%" alt="Stv"/> 
   <img id="kImage" src="" width="33%" height="100%" alt="key"/>
   <img id="fImage" src="" width="33%" height="100%" alt="Fbd"/>
</body>
</html>
Need Help diagnosing the fault in this randomization loop
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic