如何使用 JavaScript 进行计数

我一直在尝试使用 JavaScript 创建计数,但它不起作用


我做了:


function increment (){

     for (var i = 0; i <= 100;  i++){

         text.innerHTML += parseInt(i)

      }

 }


setTimeout(increment, 200)


跃然一笑
浏览 218回答 1
1回答

慕无忌1623718

您需要使用setInterval以进行实际计数,如果这是您想要实现的目标:function countUp() {&nbsp; var i = 0; // a counter which is displayed every 100ms&nbsp; &nbsp;// create interval which fires the callback every 100ms.&nbsp; &nbsp;// `interval` holds the interval ID, which is later used to clear the&nbsp; &nbsp;// interval (stop calling the callback)&nbsp; var interval = setInterval(function() {&nbsp;&nbsp; &nbsp; text.innerHTML = i++; // write `i` and increment&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // if `i` is grater than 100 then clear the interval (stop calling the callback)&nbsp; &nbsp; if (i > 100) clearInterval(interval);&nbsp; }, 100);}countUp();<div id="text"></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript