Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX loop to refresh jQueryUI ProgressBar

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.

like image 792
tedski Avatar asked Mar 02 '12 21:03

tedski


People also ask

What is ProgressBar in jQuery UI?

JqueryUI - Progressbar. Progress bars indicate the completion percentage of an operation or process. We can categorize progress bar as determinate progress bar and indeterminate progress bar. Determinate progress bar should only be used in situations where the system can accurately update the current status.

How to change the value of the progress bar in JavaScript?

var present_value = $ ( "#progress_bar" ).progressbar ( "option", "value" ); We can change the value of the progress bar like this. $ ( "#progress_bar" ).progressbar ( "option", "value", 60 ); We can change the value of the progress bar by connecting with different events.

How do I style the ProgressBar widget?

The progressbar widget uses the jQuery UI CSS framework to style its look and feel. If progressbar specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes option: ui-progressbar: The outer container of the progressbar.

What is the meaning of progress bar?

Progress bars indicate the completion percentage of an operation or process. We can categorize progress bar as determinate progress bar and indeterminate progress bar. Determinate progress bar should only be used in situations where the system can accurately update the current status.


1 Answers

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

like image 64
Jeff B Avatar answered Sep 18 '22 15:09

Jeff B