我一直在尝试使用 JavaScript 创建计数,但它不起作用我做了:function increment (){ for (var i = 0; i <= 100; i++){ text.innerHTML += parseInt(i) } }setTimeout(increment, 200)
1 回答
慕无忌1623718
TA贡献1744条经验 获得超4个赞
您需要使用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>
添加回答
举报
0/150
提交
取消
