XMLHttpRequest 2为XMLHttpRequest对象新增timeout属性,使用该属性可以设置HTTP请求时限。
xhr.timeout = 3000;
上面语句将异步请求的最长等待时间设为3000毫秒。超过时限,就自动停止HTTP请求。
与之配套的还有一个timeout事件,用来指定回调函数。
xhr.ontimeout = function(event){
console.log('请求超时!');
}
课后整理 2020-12-20
XMLHttpRequest 2为XMLHttpRequest对象新增timeout属性,使用该属性可以设置HTTP请求时限。
xhr.timeout = 3000;
上面语句将异步请求的最长等待时间设为3000毫秒。超过时限,就自动停止HTTP请求。
与之配套的还有一个timeout事件,用来指定回调函数。
xhr.ontimeout = function(event){
console.log('请求超时!');
}