我一整天都在寻找解决这个问题的方法。我喜欢在StackOverflow上找到的this Counter,但由于我没有经验使用JavaScript,我不完全确定如何阻止它。
我想出了如何设置值以及何时开始计数等,但现在我想添加一个最大值。 IE:如果计数器达到2000万,那么停止。
我尝试了以下操作,但它不起作用:
var simplicity = formatMoney(20000000);
if (amount.innerText <= simplicity){
function update() {
var current = (new Date().getTime() - start)/1000*0.36+0;
amount.innerText = formatMoney(current);
}
setInterval(update,1000);
}
else{
amount.innerText = simplicity;
}
答案 0 :(得分:1)
试试这个
var max = 20000000;
var intervalId = setInterval(function () {
var current = (new Date().getTime() - start)/1000*0.36+0;
if (current > max) {
amount.innerText = formatMoney(max);
clearInterval(intervalId);
} else {
amount.innerText = formatMoney(current);
}
}, 1000);
使用clearInterval(id)
停止间隔。