📜  在 javascript 代码示例中将超时设置为 XMLHttpRequests

📅  最后修改于: 2022-03-11 15:03:54.543000             🧑  作者: Mango

代码示例1
let xhr = new XMLHttpRequest (); 
xhr.onreadystatechange = function () {  
    if (this.readyState == 4) {  
        clearTimeout(timeout);  
        // do something with response data 
    }  
}  
let timeout = setTimeout( function () {  
    xhr.abort(); // call error callback  
}, 60*1000 /* timeout after a minute */ ); 
xhr.open('GET', url, true);  

xhr.send();