【问题标题】:Javascript simple count up with delay [duplicate]Javascript简单计数延迟[重复]
【发布时间】:2014-12-21 10:32:02
【问题描述】:

我有一个应该更改 html 元素的 JS 脚本 - 可见从 1 计数到 100。当我添加超时时,for 循环不再循环 - 直接跳转到 100。它适用于 alert 但不适用于div 变化。

var theLabel = document.getElementById("counter");
 function doSetTimeout(i) {
  setTimeout(function() {
    /*alert(i); */
    theLabel.innerHTML = i;

    }, 1000);
  }

 for (var i = 1; i <= 100; i++)
 doSetTimeout(i);

【问题讨论】:

    标签: javascript


    【解决方案1】:

    http://jsfiddle.net/hqh6Lne9/

    var i = 0;
    theLabel = document.getElementById("counter");
    var interval = setInterval(function(){ 
        if (i == 100) clearInterval(interval);
        theLabel.innerHTML = i; 
        i++;
    }, 
    1000);
    

    【讨论】:

    • 谢谢,不知道需要清除它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2012-12-04
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2013-04-08
    • 2019-02-17
    相关资源
    最近更新 更多