javascript 每 x 秒增加一次进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7657558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:49:38  来源:igfitidea点击:

increment progressbar every x seconds

javascriptjqueryjquery-ui

提问by Mythrillic

I basically need a jQuery UI progress bar to increment x amount every x seconds. After it reaches 100% it needs to run a function to get some content.

我基本上需要一个 jQuery UI 进度条来每 x 秒增加 x 量。达到 100% 后,它需要运行一个函数来获取一些内容。

Basically a timer.

基本上是一个计时器。

EDIT:I don't need code to fetch content. I already have that.

编辑:我不需要代码来获取内容。我已经有了。

回答by alex

var progressBar = $('#progress-bar'),
    width = 0;

progressBar.width(width);

var interval = setInterval(function() {

    width += 10;

    progressBar.css('width', width + '%');

    if (width >= 100) {
        clearInterval(interval);
    }
}, 1000);

jsFiddle.

js小提琴

回答by Salman A

Assuming that you're using the jQueryUI progressbar:

假设您正在使用jQueryUI 进度条

var tick_interval = 1;
var tick_increment = 10;
var tick_function = function() {
    var value = $("#progressbar").progressbar("option", "value");
    value += tick_increment;
    $("#progressbar").progressbar("option", "value", value);
    if (value < 100) {
        window.setTimeout(tick_function, tick_interval * 1000);
    } else {
        alert("Done");
    }
};
window.setTimeout(tick_function, tick_interval * 1000);

Demo here

演示在这里

回答by Alan Haggai Alavi

jQuery UIhas a progress bar widget. You can set its value inside setInterval. Run clearIntervalwhen completeevent is raised.

jQuery UI有一个进度条小部件。你可以在里面设置它的值setIntervalclearIntervalcomplete引发事件时运行。