admin管理员组文章数量:1221306
I've got a jQueryUI progressbar that should show the percentage of a query done. Oracle has a nice system table that lets you see operations that will take more than 10 seconds. I'm trying to make staggered $.ajax calls to this query in order to refresh the progress bar.
Problem is, I can either get the loops to make rapid-fire requests without any wait time, or just delay the entire JavaScript from executing.
I start the first request by clicking my "Execute" button in a jQueryUI dialog.
$("#dlgQuery").dialog({
buttons: {
Execute: function () {
$(this).dialog("close");
StartLoop();
}
}
});
I'm trying to build either the StartLoop()
function or make a recursive GetProgress()
function. Ideally, I will have a public variable var isDone = false
to act as my indicator for when to terminate the loop or stop recursively calling the function.
For simplicity I have just made a static loop that executes 100 times:
function StartLoop(){
for (var i = 0; i < 100; i++) {
GetProgress();
}
}
And here's my sample ajax request:
function GetProgress() {
$.ajax({
url: "query.aspx/GetProgress",
success: function (msg) {
var data = $.parseJSON(msg.d);
$("#pbrQuery").progressbar("value", data.value);
//recursive?
//GetProgress();
//if (data.value == 100) isDone = true;
}
});
}
So what I've found is, so far:
setTimeout(GetProgress(), 3000)
in StartLoop()
freezes Javascript, and the dialog does not close (I assume, because it will wait until the query is done).
This one, pausecomp(3000)
does much the same thing.
If I call either of these in the "success" function of my AJAX request, it gets ignored (probably because it's starting another call asynchronously).
I'm kinda stuck here, any help would be appreciated, thanks.
I've got a jQueryUI progressbar that should show the percentage of a query done. Oracle has a nice system table that lets you see operations that will take more than 10 seconds. I'm trying to make staggered $.ajax calls to this query in order to refresh the progress bar.
Problem is, I can either get the loops to make rapid-fire requests without any wait time, or just delay the entire JavaScript from executing.
I start the first request by clicking my "Execute" button in a jQueryUI dialog.
$("#dlgQuery").dialog({
buttons: {
Execute: function () {
$(this).dialog("close");
StartLoop();
}
}
});
I'm trying to build either the StartLoop()
function or make a recursive GetProgress()
function. Ideally, I will have a public variable var isDone = false
to act as my indicator for when to terminate the loop or stop recursively calling the function.
For simplicity I have just made a static loop that executes 100 times:
function StartLoop(){
for (var i = 0; i < 100; i++) {
GetProgress();
}
}
And here's my sample ajax request:
function GetProgress() {
$.ajax({
url: "query.aspx/GetProgress",
success: function (msg) {
var data = $.parseJSON(msg.d);
$("#pbrQuery").progressbar("value", data.value);
//recursive?
//GetProgress();
//if (data.value == 100) isDone = true;
}
});
}
So what I've found is, so far:
setTimeout(GetProgress(), 3000)
in StartLoop()
freezes Javascript, and the dialog does not close (I assume, because it will wait until the query is done).
This one, pausecomp(3000)
does much the same thing.
If I call either of these in the "success" function of my AJAX request, it gets ignored (probably because it's starting another call asynchronously).
I'm kinda stuck here, any help would be appreciated, thanks.
Share Improve this question asked Mar 2, 2012 at 21:59 tedskitedski 2,3113 gold badges27 silver badges48 bronze badges 2- 1 interesting question. :) I can't think of a good answer. Have you tried using setInterval at all? What happened when you tried the recursive method? – Thomas Clayson Commented Mar 2, 2012 at 22:05
- I get rapid-fire requests/responses. – tedski Commented Mar 2, 2012 at 22:15
2 Answers
Reset to default 16Instead of setTimeout(GetProgress(), 3000)
, you would want:
function StartLoop(){
for (var i = 0; i < 100; i++) {
setTimeout(GetProgress(), 3000*i);
}
}
Otherwise, all 100 will fire off after 3 seconds. Instead, you want 0, 3000, 6000, 9000, etc., i.e. 3000*i
;
Better, you could use setInterval
and clearInterval
:
var myInterval = setInterval(GetProgress(), 3000);
and in the callback, do:
$.ajax({
url: "query.aspx/GetProgress",
success: function (msg) {
var data = $.parseJSON(msg.d);
$("#pbrQuery").progressbar("value", data.value);
if (data.value == 100) {
isDone = true;
clearInterval(myInterval);
}
}
});
clearInterval
will stop it from calling GetProgress()
again. Using the setInterval
method means you don't have to know how many poll loops you need up front. It will simply continue until you are done.
Or better yet, you can call GetProgress()
from the ajax callback, with the advantage that it will only poll again once you have a response from your query:
function GetProgress() {
$.ajax({
url: "query.aspx/GetProgress",
success: function (msg) {
var data = $.parseJSON(msg.d);
$("#pbrQuery").progressbar("value", data.value);
if (data.value == 100) {
isDone = true;
} else {
setTimeout(GetProgress(), 2000);
}
}
});
}
Then just call GetProgress()
once to initiate the loop.
I believe what you want to do is call the getProgress function again when it completes.
You would do this by adding the 'complete' param to the ajax call
$.ajax({
//this is where your stuff already is
,complete: getProgress()
//we add a timeout so it doesn't run everytime it completes, only when we want to update the progress bar.
,timeout: 10000 //this is 10 seconds
});
This is a method commonly referred to as 'polling'.
本文标签: javascriptjQuery AJAX loop to refresh jQueryUI ProgressBarStack Overflow
版权声明:本文标题:javascript - jQuery AJAX loop to refresh jQueryUI ProgressBar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739263412a2155477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论