Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel jQuery load

My problem here is that my main page loads some slow asp pages into my divs through jQuery:

$("#content").load("really_slow.asp");

But if I try to go to another page on my site by clicking a link before the ASP page is done, it won't let me go until it's finished loading.

I've tried several ways, but nothing appears to work.... I tried something like this:

var request = $.ajax({
    type: 'GET',
    url: 'really_slow.asp',
    success: function(result){
        $("#content").html(result);
    }
});


$("a").click(function() { 
    request.abort();
});

with no difference...

like image 942
marcos.borunda Avatar asked Oct 10 '22 10:10

marcos.borunda


1 Answers

You're almost there, just one critical mistake in your code. It should be:

var request = $.ajax({
    type: 'GET',
    url: 'really_slow.asp',
    success: function(result){
        $("#content").html(result);
    }
});


$("a").click(function() { 
    request.abort();
});

The abort() method should be called on the jqXHR object, not the #content div. See: Abort Ajax requests using jQuery

like image 53
jli Avatar answered Oct 13 '22 11:10

jli