Random interval number
I modified this countdown script to actually count up.
Now it changes the innerHTML of the div "container" every 300 ms.
How can i change that 300ms to be a random number between 300 and 2000?
Thanks so much for all your help :P
Greetz, Camillo
var time = 1; //How long (in seconds) to countdown
开发者_如何学编程function countDown(){
time++;
gett("container").innerHTML = time;
if(time == 0){
window.location = page;
}
}
function gett(id){
if(document.getElementById) return document.getElementById(id);
if(document.all) return document.all.id;
if(document.layers) return document.layers.id;
if(window.opera) return window.opera.id;
}
function init(){
if(gett('container')){
setInterval(countDown, 300); // here instead of 300 a random number between 300 and 2000
gett("container").innerHTML = time;
}
else{
setTimeout(init, 50);
}
}
Replace with: ( 300+Math.floor(Math.random() * 1700) )
EDIT AFTER YOUR COMMENT:
function countDown(){
gett("container").innerHTML = ++time;
if(time == 0){
window.location = page;
}
else
setTimeout(countDown,( 300+Math.floor(Math.random() * 1700) ));
}
And you call it by using:
setTimeout(countDown,( 300+Math.floor(Math.random() * 1700) ))
or simply countDown()
Oh, and you should know that accepting answers will increase your chances of getting "more of them" :)
精彩评论