Friday, March 19, 2010

Create AJAX queue with jquery

1. Create a global variable for your ajax request
var ajax_request = null;
2. Disable client side caching 
 $.ajaxSetup ({ 
         cache: false 
    }); 
3. Before you start the ajax call, create an if statement that will abort any existing requests
   if (ajax_request) { // checks for an existing ajax request
        ajax_request.abort(); // aborts request
    }
 4. Finally set global variable to ajax request
    ajax_request = $.ajax({
     url: "some/service.cfm",
     type: "POST",
     data: { var1: someval1,
                var2: someval2},
     success: function(data) {
            alert("success");
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) { // execute this if it all goes boom
            alert(textStatus + errorThrown);
            return false;
            }
    });  

No comments:

Post a Comment