JS setInterval暂停和重启
function fetchData(){
$.ajax({
url:"tym_alarm.asp",
dataType:"text",
success:function(data){
$("#b")[0].innerHTML = data;
if ($("#b")[0].innerHTML!=""){
//如何暂停执行fetchData()10秒钟,10S后再setInterval(fetchData,1000)
}
}
});
}
setInterval(fetchData,1000)
</script> 展开
setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。
setInterval() 没有暂停这一说,只能清除和开启。
<title></title>
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var iCount = setInterval(GetBack, 3000);
function GetBack() {
alert("aa");
$.ajax({
type: "POST",
url: "WebForm4.aspx/GetString",
dataType: "text",
contentType: "application/json; charset=utf-8",
beforeSend: function (XMLHttpRequest) {
},
success: function (msg) {
alert("ff");
},
error: function (msg) {
alert(msg);
}
});
}
$("#cOk").click(function (e) {
clearInterval(iCount);
});
});
</script>
<div>
<a href="#" id="cOk" >sss</a>
</div>
后台代码
------------------
[WebMethod]
public static string GetString()
{
return "aa";
}