(这个是在面试中遇到的一道题目,自己不会,然后根据网上的内容,自己整理一下,以免下次再遇到)
1、是在jQuery中的话,可以直接使用abort方法
<script src = "jquery-1.4.4.js"></script>
<script>
var xhr = $.ajax({type:'POST',
url:'b.php',
data:'',
success:function(){
alert('ok');
}
})
alert(xhr);
console.log(xhr);
</script>
<button id="song">abort</button>
<script>
$(function(){
$("#song").click(function(){
alert('click');
xhr.abort();
})
})
</script>
2、原生的xhr的方法
xmlHttp.open("POST","theUrl",true);
xmlHttp.onreadystatechange=function(){
...//得到响应之后的操作
}
xmlHttp.send();
//设置8秒钟后检查xmlHttp对象所发送的数据是否得到响应.
setTimeout("CheckRequest()","8000");
function CheckRequest(){
//为4时代表请求完成了
if(xmlHttp.readyState!=4){
alert('响应超时');
//关闭请求
xmlHttp.close();
}
}
浙公网安备 33010602011771号