PDA

View Full Version : setting a random time interval for a slideshow?



newsteve
August 13th, 2007, 08:21 PM
hi,

this should be simple but its stumping me,

I am trying to make a slideshow that pulls random images at a random time interval (between 1 and 4 seconds). The images part works fine, and I can get the time function to create a random number, but that number doesnt change throughout the duration of the slideshow. I would like the duration to reset itself and changefor every picture that is displayed. Here is my code:


function allPlantPick(){

setInterval("plantPick('plant1')", plantTimer());

}

function plantTimer() {

var plantNum = (Math.random()*4000) + 1000;
return(plantNum);

}

//this function works fine:
function plantPick(myFoo){

var plantChoices=["images/p1.jpg","images/p2.jpg","images/p3.jpg"];

document.getElementById(myFoo).style.backgroundIma ge = "url(" + plantChoices[ Math.floor(Math.random()*plantChoices.length) ] + ")";
}

icio
August 15th, 2007, 07:00 AM
I think what you want to do is, instead of calling `setInterval`, call `setTimeout` which will only run once, and then call `allPlantPick` at the end of every `plantPick`. Eg,


function allPlantPick(){

setTimeout("plantPick('plant1')", plantTimer());

}

function plantTimer() {

var plantNum = (Math.random()*4000) + 1000;
return(plantNum);

}

//this function works fine:
function plantPick(myFoo){

var plantChoices=["images/p1.jpg","images/p2.jpg","images/p3.jpg"];

document.getElementById(myFoo).style.backgroundIma ge = "url(" + plantChoices[ Math.floor(Math.random()*plantChoices.length) ] + ")";
allPlantPick();
}

Note: "I think"