0

I can't seem to get this ajax request to cancel when the user leaves the page. I'm not sure why. The problem is that the client is waiting for the request to complete before the user leaves the page. This is a large request in that it takes about 5 - 7 seconds to complete. As a result, if a user navigates away from this page while a request is under way they have to wait that time before the next page is loaded. Here is my code.

window.loader = setInterval(load_queued_tasks, 15000);
    // function that should run every 15 seconds //
    function load_queued_tasks() {

        window.loader_ajax_request = $.ajax({
                    url: "coolpage.php",
                    type: 'GET',
                    success: function(data) {
            //lots of exciting actions
                      })     
            },
                    error: function() {
                       return false;
                    }
                }); //end ajax call
     }

    window.onbeforeunload = function (e) {
        //clear the interval timer
        window.clearInterval(window.loader);

        //abort any running ajax request
        window.loader_ajax_request.abort(); 
    };
  • 5
    Note that most servers just won't stop processing requests also if they were canceled. – jjurm May 22 '13 at 15:30
  • This thread might be helpful:- http://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery – tinyd May 22 '13 at 16:16
  • I am using code from that link, thank you. Could the issue be server side? ie the server is waiting until the processing of the coolpage.php is complete before presenting the next page? – user1886368 May 22 '13 at 16:25
  • Got it, just needed to read a bit more. Thank you. – user1886368 May 22 '13 at 16:30

0 Answers0