我一直在尝试使用 JavaScript 创建计数,但它不起作用
我做到了:
function increment (){
for (var i = 0; i <= 100; i++){
text.innerHTML += parseInt(i)
}
}
setTimeout(increment, 200)
最佳答案
如果您想要实现的话,您需要使用 setInterval
来进行实际计数:
function countUp() {
var i = 0; // a counter which is displayed every 100ms
// create interval which fires the callback every 100ms.
// `interval` holds the interval ID, which is later used to clear the
// interval (stop calling the callback)
var interval = setInterval(function() {
text.innerHTML = i++; // write `i` and increment
// if `i` is grater than 100 then clear the interval (stop calling the callback)
if (i > 100) clearInterval(interval);
}, 100);
}
countUp();
<div id="text"></div>
关于javascript - 如何使用 JavaScript 进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58652080/