How to cancel/abort jQuery AJAX request?

后端 未结 8 2030
时光说笑
时光说笑 2020-11-22 09:30

I\'ve an AJAX request which will be made every 5 seconds. But the problem is before the AJAX request if the previous request is not completed I\'ve to abort that request and

8条回答
  •  囚心锁ツ
    2020-11-22 10:25

    When you make a request to a server, have it check to see if a progress is not null (or fetching that data) first. If it is fetching data, abort the previous request and initiate the new one.

    var progress = null
    
    function fn () {    
        if (progress) {
            progress.abort();
        }
        progress = $.ajax('ajax/progress.ftl', {
            success: function(data) {
                //do something
                progress = null;
            }
        });
    }
    

提交回复
热议问题