循环内时,Progressbar的进度不会更新 [英] Progressbar's progress not updating when inside a loop

查看:552
本文介绍了循环内时,Progressbar的进度不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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]);
}

我添加了if语句,因为我不想继续更新每个循环上的进度条.循环运行了将近222000次.是否有更好的逻辑来更新进度?

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?

为什么它从不输入if语句?

Why does it never enter the if statement?

推荐答案

您可以使用它,对您的代码进行一些优化:

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..
}

您将使用for循环吗?没有任何暂停"此循环,因此它将非常快速地运行,您可能会看到它以100%的速度增长,而不是不断增长.

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

当您的其他代码"正在运行时,您可以改为调用一个函数(而不是for循环)来更新进度栏:

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

这篇关于循环内时,Progressbar的进度不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆