停止jq中的ajax请求用abort()函数
ps:本人亲测,阿里云2核4G5M的服务器性价比很高,新用户一块多一天,老用户三块多一天,最高可以买三年,感兴趣的可以戳一下:阿里云折扣服务器
停止JQ中的ajax请求用abort()函数
<html>
<head>
<mce:script type="text/javascript"><!--
var currentAjax = null;
function startAjax(){
//方法就是将XHR对象指向currentAjax,再调用currentAjax的.abort()来中止请求
currentAjax = $.ajax({
type:'POST',
beforeSend:function(){},
url:'test.php',
data:'username=xxx',
dataType:'JSON',
error:function(){alert('error')},
success:function(data){alert(data)}
});
}
function stopAjax(){
//如若上一次AJAX请求未完成,则中止请求
if(currentAjax) {currentAjax.abort();}
}
// --></mce:script>
</head>
<body>
<input type="button" value="触发请求" onclick="startAjax()" />
<input type="button" value="停止请求" onclick="stopAjax()" />
</body>
</html>