$("#start").toggle(function(){
$("#start").val("停止");
$("#load_realTest").show();
$("#real_table").show().empty();
realTest(true);
},function(){
number=1;
$("#start").val("开始");
$("#load_realTest").hide();
});
function realTest(state) {
if(state){
var maxTime = parseInt($("#maxTime").val());
var timeInterval = parseInt($("#timeInterval").val());
var param1 = $("#taskParamForm").serialize();
var param2 = $("#realTestForm").serialize();
$.ajax( {
type : "post",
url : "tp_testingRealTimeUI.do",
data : param1 + "&" + param2 + "&id=" + number,
timeout:20000,
contentType : "application/x-www-form-urlencoded;charset=utf-8",
success : function(data) {
$("#real_table").append(data);
document.getElementById("expansion").scrollIntoView();
if(number==maxTime){
$("#start").val("开始");
$("#load_realTest").hide();
number=1;
}else{
number++;
window.setTimeout("realTest(true)",timeInterval);
}
},
error : function(XMLHttpRequest, textStatus, errorThrown){ 
if(textStatus=="timeout"){
alert("连接超时!");
number++;
realTest(true);
return;
}
number++;
$("#load_realTest").hide();
alert("程序出现错误,请联系管理员!", "错误");
}
});
}
}我要做的是一个实时的功能,通过ajax实时的往后台发送请求,然后显示,现有有两个问题:
1、就是在ajax中添加timeout属性,处理连接超时,也不知道对不对,请各位大神看下。
2、实时测试开始后,按钮会变成停止,可是如何来停止ajax往后台发送请求呢?