Phil Jackson
Phil Jackson

Reputation: 10288

how to kill ajax requests

I am trying to create a script to keep running even on fail until a certain word is returned. The bottom function will run for around 30 seconds, stop due to the PHP script (which is what i want) and then jquery will trigger it to run again and again (until the certain trigger is returned). The top function get_data(); will run every second and a half after itself has finished the request (It basically finds out at what stage the the other request is at.

My problem being to keep things tidy, when the request is activated again, it kicks up the get_data() function again which is not needed unless an error occurs in which case it does. To overcome this I simply wanted to terminate all ajax request and the start over again (the get data function only runs a small php script which at max will take half a second to process and the other about 10 seconds so it will not be doing much harm by doing this) but I cant seem to get it to terminate the ajax requests, i just keep having multiple get_data() requests firing off in all directions...

$(document).ready(function() {
    var number = 0;
    var requests = [];

    function kill_requests( requests ){
        $.each( requests, function( i, v ){
            v.abort();
        })
    }
    function get_data( url, requests ){
        $('#requests').html( number );
        requests.push( $.ajax({ 
            type: 'POST', url: './scrape/ajaxGetData.php', data: 'url=' + encodeURIComponent(url), cache: false, timeout: 10000,
            error : function(){ },
            success: function(html){ 
                if( html.substr(0,12) == '<!-- die -->' ) {
                    kill_requests( requests );
                    $("#result").html('<p>Complete...</p>' + html );
                }else{
                    $("#result").html(html);
                    window.setTimeout( function(){ number++; get_data( url, requests ) }, 2000 );
                }
            }                           
        }));
    }
    $("input[name=submit]").live( "click", function(){
        var url = $("input[name=url]").val();
        requests.push( $.ajax({ 
            type: 'POST', url: './index.php', data: 'submit=true&url=' + encodeURIComponent(url), cache: false,
            error : function(){ 
                kill_requests( requests ); 
                $("input[name=submit]").trigger('click'); 
            },
            success: function(html){ 
                $("#result2").html(html); 
                if( html.substr(0,12) != '<!-- die -->' ) {
                    kill_requests( requests );
                    $("input[name=submit]").trigger('click');
                }
            }                           
        }));
        get_data( url, requests );
    });
});

Upvotes: 3

Views: 1510

Answers (2)

John Kramlich
John Kramlich

Reputation: 2260

Look into the AJAX Manager Plugin here:

http://plugins.jquery.com/project/AjaxManager

and here:

http://www.protofunc.com/scripts/jquery/ajaxManager/

It allows you to cache requests, prevent double requests to the same URL and abort requests, among other capabilities.

Upvotes: 1

ngduc
ngduc

Reputation: 1415

Have you try jquery abort() function?

quoted "calling .abort() on the object will halt the request before it completes."

http://api.jquery.com/jQuery.ajax/

Upvotes: 1

Related Questions