Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancelling the current ajax request via a button click event!

I'd like cancel if my ajax request is taking long time with slower connections.

I show an overlay when .ajaxStart() and if takes longer than five secons (i managed with setInterval) a button appears on overlay that should cancel the current ajax request.

heres some of my code:

jqXHR = $.ajax({
    url: 'includes/inc_ajax.php',
    type: 'POST',
    data: '&ajax=1&action=7',
    success: function (txt) {
        var cntr = $.parseJSON(txt);
        $("#onlineppl").html(cntr.online);
        $("#todayppl").html(cntr.bugun);
        $("#totalppl").html(cntr.toplam);
    }
});

$("#abortAjax").button({ icons: {secondary: "ui-icon-cancel"} }).click(function() {
    $(jqXHR).abort();
});

Do I have to assign the $.ajax() function to a variable to abort it? I have many ajax request called by ajax too so i guess the browser can't track the same var name that causes the abort method not to work.

This is my first question here and I am a bit confused. Any help appreciated. Thanks.

like image 787
Algün Avatar asked Dec 08 '25 03:12

Algün


1 Answers

Well, you culd try:

jqXHR.abort();

Whitout the $()

Note its not tested, i saw it somewhere

EDIT

Aah found it:

Stop all active ajax requests in jQuery

like image 187
Limpan Avatar answered Dec 10 '25 18:12

Limpan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!