मुझे पता है कि आपने विशेष रूप से Mootools
समाधान के लिए नहीं पूछा है, लेकिन मैंने ऐसा कुछ किया है जिसे मैंने सोचा था कि मैं दूसरों के संदर्भ के लिए यहां साझा करूंगा। मैं विभिन्न अनुरोध विधियों को ओवरराइड करके इसे प्राप्त करता हूं। मैं वर्तमान में संस्करण 1.5.1 का उपयोग कर रहा हूं, लेकिन यह या कुछ अन्य संस्करणों पर समान काम करना चाहिए।
(function() {
var requestCounter = 0,
send = Request.prototype.send,
cancel = Request.prototype.cancel,
onSuccess = Request.prototype.onSuccess,
onFailure = Request.prototype.onFailure,
timeout = Request.prototype.timeout;
var increment = function() {
++requestCounter;
};
var decrement = function() {
--requestCounter;
if (requestCounter < 0) {
requestCounter = 0;
}
};
Request.implement({
send: function() {
increment();
return send.apply(this, arguments);
},
cancel: function() {
decrement();
return cancel.apply(this, arguments);
},
onSuccess: function() {
decrement();
return onSuccess.apply(this, arguments);
},
onFailure: function() {
decrement();
return onFailure.apply(this, arguments);
},
timeout: function() {
decrement();
return timeout.apply(this, arguments);
}
});
Request.getRunningCount = function() {
return requestCounter;
};
})();
अब हर बार एक अनुरोध किया जाता है, काउंटर भी वृद्धि होगी और जब यह पूरा हो गया है, यह घटती जाएगी।
var request1 = new Request(...);
var request2 = new Request(...);
request1.send();
request2.send();
console.log(Request.getRunningCount()); // 2
request1.cancel();
console.log(Request.getRunningCount()); // 1
// request2 completes
console.log(Request.getRunningCount()); // 0
बहुत अच्छा तरीका! प्रेषण() विधि को "पुनः परिभाषित" करके, आप बेसमेंट में काम कर रहे हैं, जिससे आप * किसी * XMLHttpRequest का ट्रैक रखने में सक्षम होते हैं - इससे कोई फर्क नहीं पड़ता कि कौन सा ढांचा या एडन जारी किया गया है। – rplantiko
क्या इसके बाद पुरानी Send.apply लाइन को स्थानांतरित करना समझ जाएगा .addEventListener? इवेंट श्रोता जोड़ा जाने से पहले प्रतिक्रिया वापस आने के लिए असंभव प्रतीत होता है, लेकिन शायद यह सैद्धांतिक रूप से संभव है? –
यह ठीक काम करता है, धन्यवाद! – Akuma