gpt4 book ai didi

javascript - 随机间隔数

转载 作者:行者123 更新时间:2023-11-28 16:34:24 25 4
gpt4 key购买 nike

我修改了这个倒计时脚本以实际计数。

现在它每 300 毫秒更改一次 div“容器”的innerHTML。

如何将 300ms 更改为 300 到 2000 之间的随机数?

非常感谢您的帮助:P

再见,卡米洛

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);
}
}

最佳答案

替换为:( 300+Math.floor(Math.random() * 1700) )

发表评论后进行编辑:

function countDown(){
gett("container").innerHTML = ++time;
if(time == 0){
window.location = page;
}
else
setTimeout(countDown,( 300+Math.floor(Math.random() * 1700) ));
}

你可以使用以下方式调用它:

setTimeout(countDown,( 300+Math.floor(Math.random() * 1700) ))

或者简单地countDown()

哦,你应该知道接受答案会增加你获得“更多答案”的机会:)

关于javascript - 随机间隔数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4593562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com