简体   繁体   English

如何更新进度条?

[英]How to update progress bar?

I'm trying to update the value of the jQuery UI Progressbar . 我正在尝试更新jQuery UI Progressbar的值。

After the initialization, I run some loops. 初始化后,我运行一些循环。 After each of them, I want the progress bar to update its value. 在每个之后,我希望进度条更新其值。 But it's only displayed at the very end with its final value, so I don't see each step: 但它只显示在最后的最终值,所以我没有看到每一步:

$("#progressbar").progressbar();

for (i = 0; i < 10000000; i++) {
  // takes some time...
}
$("#progressbar").progressbar("value", 25);

for (i = 0; i < 10000000; i++) {
  // takes some time...
}
$("#progressbar").progressbar("value", 50);

for (i = 0; i < 10000000; i++) {
  // takes some time...
}
$("#progressbar").progressbar("value", 75);

for (i = 0; i < 1000000000; i++) {
  // takes some time...
}
$("#progressbar").progressbar("value", 100);

Here is a fiddle . 这是一个小提琴

尝试在每个函数的末尾添加进度条值。

The only reliable way to simulate a time interval for test purposes in Javascript is to use setInterval and setTimeout. 在Javascript中为测试目的模拟时间间隔的唯一可靠方法是使用setInterval和setTimeout。 Loops run too quickly. 循环运行得太快。

This is what you can do if you want to ensure the steps execute in order. 如果要确保按顺序执行步骤,可以执行此操作。 Just replace the setTimeout calls with what you actually want to do. 只需将setTimeout调用替换为您实际想要执行的操作即可。

function updateProgressbar($bar, value) {
    $bar.progressbar("value", value);
}

function step1() {
    setTimeout(function() {
    console.log('this is step 1');
    updateProgressbar($("#progressbar"), 25);
    step2();
  }, Math.random() * 2000 + 250);
}

function step2() {
    setTimeout(function() {
    console.log('this is step 2');
    updateProgressbar($("#progressbar"), 50);
    step3();
  }, Math.random() * 2000 + 250);
}

function step3() {
    setTimeout(function() {
    console.log('this is step 3');
    updateProgressbar($("#progressbar"), 75);
    step4();
  }, Math.random() * 2000 + 250);
}

function step4() {
    setTimeout(function() {
    console.log('this is step 4');
    updateProgressbar($("#progressbar"), 100);
  }, Math.random() * 2000 + 250);
}

$("#progressbar").progressbar();

console.log($("#progressbar").data('value'));

step1();

Demo 演示

In this case, each step is called inside the previous one to ensure they will be called in order from 1 to 4. 在这种情况下,每个步骤都在前一个步骤内调用,以确保它们将按顺序从1到4进行调用。

On the other hand, if you want all steps to be fired at the same time (ie independent ajax requests) this is what you can do. 另一方面,如果您希望同时触发所有步骤(即独立的ajax请求),这就是您可以做的。

function updateProgressbar($bar, step) {
    progress += step;
    $bar.progressbar("value", progress);
}

function step1() {
    setTimeout(function() {
    console.log('this is step 1');
    updateProgressbar($("#progressbar"), 25);
  }, Math.random() * 3000 + 1000);
}

function step2() {
    setTimeout(function() {
    console.log('this is step 2');
    updateProgressbar($("#progressbar"), 25);
  }, Math.random() * 3000 + 1000);
}

function step3() {
    setTimeout(function() {
    console.log('this is step 3');
    updateProgressbar($("#progressbar"), 25);
  }, Math.random() * 3000 + 1000);
}

function step4() {
    setTimeout(function() {
    console.log('this is step 4');
    updateProgressbar($("#progressbar"), 25);
  }, Math.random() * 3000 + 1000);
}

$("#progressbar").progressbar();

var progress = 0;

step1();
step2();
step3();
step4();

Demo 演示

In this example, all four steps are fired at the same time but execute at random times. 在此示例中,所有四个步骤同时触发,但随机执行。 The updateProgressbar function receives 2 parameters, first is the jQuery instance of the progressbar, second is the increase in progress. updateProgressbar函数接收2个参数,第一个是进度条的jQuery实例,第二个是进度增加 Watch the console to keep track of the execution. 观察控制台以跟踪执行情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM