How do I use one method to terminate all ajax requests on this page? Abort An ajax object calls five ajax times and five ajax calls five abort times

  javascript, question

Can ajax of the whole page be terminated in one call?

var ajax1=$.ajax({});
var ajax2=$.ajax({});
var ajax3=$.ajax({});
var ajax4=$.ajax({});
var ajax5=$.ajax({});

To terminate these five ajax requests, I need to
ajax1.abort()
ajax2.abort()
ajax3.abort()
ajax4.abort()
ajax5.abort()

Is there a method that can be called once

$.ajax({});
$.ajax({});
$.ajax({});
$.ajax({});
$.ajax({});

Because I now have to modify the entire project in such a large amount that I can only find a unified way to put it in the template page and use it on every page.

The specific method has already been mentioned by other respondents: put ajax objects into an array.

However, I estimate that the subject is now facing an existing project, and there are many changes and heavy workload in revising various parts. Then I offer a method with the smallest possible changes:

Modify$.ajaxMethods.

Specific code:

var ajaxs = [];
 var oldAjax = $.ajax;
 
 $.ajax = function() {
 var args = Array.prototype.slice.call(arguments);
 var ajax = oldAjax.apply(this, args);
 ajaxs.push(ajax);
 return ajax;
 bracket
 
 //add abortAll function.
 function abortAll() {
 $.each(ajaxs, function(i, ajax) {
 ajax.abort();
 });
 bracket

In this way, there is no need to modify every call. When cancel, only need to adjustabortAll()Just do it.

For reference only.