Progressbar's progress not updating when inside a loop

一曲冷凌霜 提交于 2019-12-13 19:31:40

问题


for (var i = 0; i < dataArray.length; i++) {
        if(((i/dataArray.length)*100)%10 == 0)
            $("#progressbar").progressbar({ value: (i / dataArray.length) * 100 });
        if (resultArray.indexOf(dataArray[i]) == -1) // check for duplicates
            resultArray.push(dataArray[i]);
}

I added the if statement because I dont want to keep updating the progress bar on each loop. the loop runs almost 222000 times. Is there a better logic to update the progress?

Why does it never enter the if statement?


回答1:


You can use this, just a bit optimized from your code:

prog_bar = $("#progressbar");
for (var i = 0; i < dataArray.length; i++) {
        if(i%100 == 0)
        prog_bar.progressbar({ value: (i / dataArray.length) * 100 });
        //other code..
}

Will you use a for loop? there is nothing "pausing" this loop so it will run very fast and you might just see it at 100%, instead of growing.

Demo here

You could instead call a function (instead of a for loop) to update the progress bar, as your "other code" is running:

var i = 0;
function update_progress_bar() {
    if (i % 100 == 0) {
        prog_bar.progressbar({
            value: (i / 10000) * 100
        });
    }
    i++;
}

Something like this fiddle



来源:https://stackoverflow.com/questions/18546552/progressbars-progress-not-updating-when-inside-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!