Get latest ajax request and abort others

后端 未结 3 1670
鱼传尺愫
鱼传尺愫 2021-01-04 21:50

I have been searching and this problem seems simple but cannot find answer. I have multiple request calling different url. But for each url, I only want the result once and

3条回答
  •  孤街浪徒
    2021-01-04 22:02

    This explains how to use jQuery to do an ajax call and how to abort it. All you need to do is create an array that stores each request. You could then cancel the previous request while adding the new one.

    ajaxRequests = new Array();
    
    queueRequest = function() {
        if(ajaxRequests[ajaxRequests.length - 1]) {
            ajaxRequests[ajaxRequests.length - 1].abort();
        }
    
        ajaxRequests[ajaxRequests.length] = //Insert New jQuery AJAX call here.
    }
    

提交回复
热议问题