我正在尋找一種方法來改變我的javascript數字計數器的工作方式。現在它每秒都會計數,但我希望它在1秒到15秒之間的隨機時間內進行計數。將間隔時間更改爲隨機?
function startCount(){
timer = setInterval (count, 1000);
}
function count(){
var el = document.getElementById('counter');
var currentNumber = parseFloat(removeCommas(el.innerHTML));
el.innerHTML = addCommas(currentNumber+1);
}
function addCommas(nStr){
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2 + '.00';
}
function removeCommas(aNum){
//remove any commas
aNum=aNum.replace(/,/g,"");
//remove any spaces
aNum=aNum.replace(/\s/g,"");
return aNum;
}
有人可以幫助我將計數時間更改爲隨機數嗎?
你想要一個隨機但固定的時間間隔,還是應該每個新的超時時間都是隨機的? –