js循环在控制台中工作,但不在dom中更新

时间:2014-01-09 06:09:54

标签: javascript jquery

为什么这个循环不起作用?

function progress_metro(from, to)
{
    console.log(from+" "+to);
    for(var i = from; i <= to; i++)
    {
        console.log("asdasdasd - "+i+" "+from+" "+to);
        setTimeout(function() {$(".percentage").text(i+"%")},i+100);
    }
}

控制台日志

asdasdasd - 26 26 50 viewCountry.php?country=albania:110
asdasdasd - 27 26 50 viewCountry.php?country=albania:110
asdasdasd - 28 26 50 viewCountry.php?country=albania:110
asdasdasd - 29 26 50 viewCountry.php?country=albania:110
asdasdasd - 30 26 50 viewCountry.php?country=albania:110
asdasdasd - 31 26 50 viewCountry.php?country=albania:110
asdasdasd - 32 26 50 viewCountry.php?country=albania:110
asdasdasd - 33 26 50 viewCountry.php?country=albania:110
asdasdasd - 34 26 50 viewCountry.php?country=albania:110
asdasdasd - 35 26 50 viewCountry.php?country=albania:110
asdasdasd - 36 26 50 viewCountry.php?country=albania:110
asdasdasd - 37 26 50 viewCountry.php?country=albania:110
asdasdasd - 38 26 50 viewCountry.php?country=albania:110
asdasdasd - 39 26 50 viewCountry.php?country=albania:110
asdasdasd - 40 26 50 viewCountry.php?country=albania:110
asdasdasd - 41 26 50 viewCountry.php?country=albania:110
asdasdasd - 42 26 50 viewCountry.php?country=albania:110
asdasdasd - 43 26 50 viewCountry.php?country=albania:110
asdasdasd - 44 26 50 viewCountry.php?country=albania:110
asdasdasd - 45 26 50 viewCountry.php?country=albania:110
asdasdasd - 46 26 50 viewCountry.php?country=albania:110
asdasdasd - 47 26 50 viewCountry.php?country=albania:110
asdasdasd - 48 26 50 

谢谢!

P.S。谢谢你们,伙计们!我喜欢这个社区! Problem jsfiddle - 好的,谢谢分享

5 个答案:

答案 0 :(得分:1)

很可能是因为dom还没准备好,但是在打开控制台时它已经准备好了。你应该:

function progress_metro(from, to)
{
    ...
}

$(function(){  //Start calling when document is ready
   progress_metro(...);
});

希望这会有所帮助。干杯

答案 1 :(得分:1)

在你的情况下,我认为for循环一执行就会覆盖先前的写入值。我在这里做了一个转机。

我已将所有值都推送到一个数组中,然后通过它循环创建一个与您想要的类似的功能。

<强> fiddle here

我还要提供代码供您参考。

var arr=[];
function progress_metro(from, to)
{
     console.log(from+" "+to);
     for(var i = from; i <= to; i++)
     {
         arr.push(i);
     }
        var j = 0;
        setInterval(function() { 
          console.log("asdasdasd - "+j+" "+from+" "+to);
          $(".percentage").text(arr[j++]+"%")},1000);
}
progress_metro(5,100);

根据您的要求进行编辑。

希望这有帮助!

答案 2 :(得分:1)

我认为你want this JsFiddle Demo.

由于i的循环值中的闭合效果得到更新,你只能看到最后一次,并且settimeout中的时间以毫秒为单位,100 ms太快看不到响应。

function progress_metro(from, to) {
    console.log(from + " " + to);
    for (var i = from; i <= to; i++) {

        console.log("asdasdasd - " + i + " " + from + " " + to);
         doSetTimeout(i);
    }
}
function doSetTimeout(i) {
  setTimeout(function() {setDom(i)}, i*100);
}

function setDom(i) {        
    $(".percentage").text(i + "%");
}
progress_metro(5, 100);

答案 3 :(得分:1)

here is the lastest code with some tweaks 


from=1000,to=1100; // dummy value say value 
function progress_metro(from, to,i){
    console.log("asdasdasd - "+i+" "+from+" "+to);
    setInterval(function() {
    $(".percentage").append(i+"%"+"<br>");
    },1);
 }
for(var i = from; i <= to; i++){
progress_metro(from,to,i);
}

http://jsfiddle.net/T4GgQ/

&安培;请下次确保你包括小提琴:)因为它很难理解你想要的

答案 4 :(得分:1)

这里有一个建议尝试在循环外编写函数:

function progress_metro(from, to) {
    console.log(from+" "+to);
    for(var i = from; i <= to; i++) {
        console.log("asdasdasd - "+i+" "+from+" "+to);
        setTimeout(print(i), i+100);
    }
}

function print(i) {
    $(".percentage").append(i+"% <br/>");
}

progress_metro(0,100);