views:

307

answers:

3

I have a "processing..." div overlay that I show with $('').show() prior to making ajax calls and hide on completion. The problem I'm running into is that if I want to show the div and then make several ajax calls prior to hiding the div again it seems to queue the events in Internet Explorer (7) and Chrome (2.0.172.39), but I get the expected result in Firefox (3.0.13). When there's a single ajax call it works fine. The code is something like this:

<div id="foo">processing...</div>
<button onclick="performSingle()" />  <-- This behaves like it's supposed to
<button onclick="performMultiple()" /> <-- This queues the events in IE/Chrome.

<script>
$(document).ready(function(){
    $('#foo').hide();
});

function singleAjaxCall() {
    $.ajax({
        url: ...,
        type: "GET",
        async: false, //There is a reason for this.
        success: function() {}
    });
}

function multipleAjaxCalls() {
    singleAjaxCall();
    singleAjaxCall();
    singleAjaxCall();
    singleAjaxCall();
    singleAjaxCall();
    singleAjaxCall();
    singleAjaxCall();
    singleAjaxCall();
}

function performSingle(){
    $('#foo').show();
    singleAjaxCall();
    $('#foo').hide();
}

function performMultiple(){
    $('#foo').show();
    multipleAjaxCalls();
    $('#foo').hide();
}
</script>
A: 

Perhaps you should signal processing by attaching to the complete event of the individual ajax calls? If you daisy chain your ajax calls, that will force it to serialize and behave as I believe you want it to. Make sure to have the final method's complete hide the dialog, as the ajax calls won't block.

Stefan Kendall
A: 

You can attach a function to call when the show is complete.

$('#foo').show( 0, function(){multipleAjaxCalls();} )

http://docs.jquery.com/Effects/show

James Wiseman
The `function` passed is ignored if you pass `0` as you've done.
Crescent Fresh
Is it now. Thanks
James Wiseman
A: 

Yeah, try nesting your Ajax calls. Something like this -

function singleAjaxCall() {
    $.ajax({
        url: ...,
        type: "GET",
        async: false, //There is a reason for this.
        success: myCallback1
    });
}


function myCallback1($data)
{
    if(some condition){
    performSingle();
    }
}
Arpit Tambi