0

I am using jquery ajax load() function to refresh DIV content every 5 sec. But, after sometime multiple xhr request pending and slow down the process to freez browser! How to abort those pending request after waiting 5 sec/request. Or any better solution to fix this issue?

    $(document).ready(
    var xhr;
    var fn = function() {
        if (xhr && xhr.readyState != 4) {
            xhr.abort();
        }
        xhr = $.ajax({});
    };
    var interval = setInterval(fn, 5000);
);

Some developer talking about above mentioned code block but i could not fix my problem with it.
HERE IS MY CODE :

$(document).ready(function() {
    $('.slot1_clicker').on('click', function() {
        $("#slot1").load(" #slot1 ", function() {
            $.getScript("/files/po.js");
        });
    });
});
setInterval(function() {
    $('.slot1_clicker').trigger('click');
}, 5000);
  • xhr.abort(); => https://stackoverflow.com/questions/4551175/how-to-cancel-abort-jquery-ajax-request/4551178 – Mister Jojo May 20 '19 at 03:20
  • 1
    Don't use `setInterval` for things that can potentially run longer than the specified interval. Instead, you should use `setTimeout` to start the task, then call `setTimeout` again inside the task at the very end to queue up the next run. – Herohtar May 20 '19 at 06:47
  • `setInterval(function() { $("#slot1").load(" #slot1 "); }, 2000 ); setTimeout(function() { $("#slot1").load(" #slot1"); }, 2000 );` You mean i should write like this skip the XHR pending request? or what you meant? Can you please highlight with correct syntax – tashi tsering May 20 '19 at 07:49

0 Answers0