[英]How do I find/profile what JavaScript is being executed “right now” using Firebug or any other way?
[英]How can I know that no one AJAX request is being executed right now?
我怎么知道現在沒有一個 AJAX 請求正在執行,或者所有 AJAX 請求都已完成? 我需要寫一個if
語句。 我需要 js 本機或 jquery。 喜歡:
if(```no ajax pending```){
//do some
}
謝謝!
您可以在 jquery 中設置跟蹤 ajax 請求的事件
https://api.jquery.com/ajaxstart/
https://api.jquery.com/ajaxstop
let ajaxRunning = false ;
$( document ).ajaxStart(function() {
ajaxRunning = true;
}).ajaxStop(function() {
ajaxRunning = false;
});
if (!ajaxRunning) {
// nothin doin
}
當所有請求都完成時, ajaxStop
被觸發
圍繞fetch
或$.ajax
或任何您使用的將每個請求添加到 Set 的包裝器,並檢查集合的大小。
const requests = new Set();
const makeReq = (...args) => {
const initialRequest = fetch(...args);
const bodyRequest = request
.then(res => res.text())
.finally(() => {
requests.delete(bodyRequest);
});
requests.add(bodyRequest);
return initialRequest;
};
然后,在發出請求時,始終使用makeReq
而不是fetch
。 不需要 jQuery。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.