多个ajax请求和进度条

发布于 2024-09-01 00:35:46 字数 947 浏览 2 评论 0原文

在下面的代码中,我创建了一个进度条,并在处理 ajax 请求时显示其进度。

我只是通过在 cnt 计数器变量中添加 5 来伪造此处显示的进度,然后我做了一个 检查计数器何时达到 90。此时,如果请求未成功执行,那么我将暂停/禁用进度栏,每当响应到来时,我将以 100 完成整个进度栏。

现在的问题是我想添加多个进度酒吧,因为我正在触发多个 ajax 请求。因此,以下是仅针对一个请求和一个进度条实现的代码,但我希望它用于多个请求。由于这里使用全局变量来检查响应和计时器 ID,所以我不知道我可以如何处理多个请求

var cnt=0;
var res=null;

function getProgress(data){

res=data;

}
var i =0;
$('#start').click(function(){

i = setInterval(function() {

if(res!=null)
{

    clearInterval(i);
    $("#pb1").progressbar( "option", "value", cnt=cnt+100 );
}
var value = $("#pb1").progressbar("option", "value");
if(value >=90 && res==null){
$("#pb1").progressbar("option", "disable");
}
else{
$("#pb1").progressbar( "option", "value", cnt=cnt+5 );
}

},2500);

$.ajax({
url: 'http://localhost/beta/demo.php',
success: getProgress
});


});


$("#pb1").progressbar({

value: 0 ,
change: function(event, ui) {

if(res!=null)
clearInterval(i);

}
});

In a following piece of code i am create a progress bar and showing its progress as the ajax request get processed.

i am faking the progress shown here just by adding 5 in to cnt counter variable after that i made a
check when counter reach to 90. at this point if the request is not executed successfully then i will pause/disable the progress bar and whenever response come i will complete the whole progress bar with 100.

now the problem is i want to add multiple progress bar as i am firing multiple ajax request. so following is the code to implement only for one request and one progress bar but i want it for more than one. as global variables are used over here for checking response and timer id so i don't know how well i can handle it for multiple request

var cnt=0;
var res=null;

function getProgress(data){

res=data;

}
var i =0;
$('#start').click(function(){

i = setInterval(function() {

if(res!=null)
{

    clearInterval(i);
    $("#pb1").progressbar( "option", "value", cnt=cnt+100 );
}
var value = $("#pb1").progressbar("option", "value");
if(value >=90 && res==null){
$("#pb1").progressbar("option", "disable");
}
else{
$("#pb1").progressbar( "option", "value", cnt=cnt+5 );
}

},2500);

$.ajax({
url: 'http://localhost/beta/demo.php',
success: getProgress
});


});


$("#pb1").progressbar({

value: 0 ,
change: function(event, ui) {

if(res!=null)
clearInterval(i);

}
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哎呦我呸! 2024-09-08 00:35:46

使用对象而不是变量怎么样?您创建一个包含所有变量的对象,然后创建与进度条一样多的副本。

更多信息请参见:http://www.javascriptkit.com/javatutors/oopjs.shtml

How about using Objects instead of variables? You create an object with all the variables in it and then just create as many copies as progress bars you have.

More info here: http://www.javascriptkit.com/javatutors/oopjs.shtml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文