躍然一笑
2022-10-27 14:47:33
有沒有辦法在隨機(jī)秒后將數(shù)字增加 1。例如,基值是 10。所以它會在隨機(jī)秒后變化 1(即隨機(jī) 1/2/3/4/5 秒)。我試過這樣:var startVal = 10;setInterval(function(){ var theVal = startVal + 1; startVal = theVal;}, Math.round(Math.random() * (6000 - 2000)) + 2000);數(shù)量在增加,但時間不是隨機(jī)的。請幫忙。
2 回答

猛跑小豬
TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個贊
如果您希望在每次迭代中更改時間,則需要使用 setTimeout。
var startVal = 10;
function increase() {
setTimeout(function(){
var theVal = startVal + 1;
startVal = theVal;
increase();
console.log(startVal);
}, Math.round(Math.random() * (6000 - 2000)) + 2000);
}
increase()

翻翻過去那場雪
TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個贊
這是一個工作示例。將在 0,1 或 2 秒后將數(shù)字加一
let number = 10;
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function addNumber() {
number++;
console.log(number)
}
setTimeout(addNumber, getRandomInt(3) * 1000)
添加回答
舉報(bào)
0/150
提交
取消