1
我setIntervel()有一個函數,我想停止這個函數的執行explicitly.How我能做到這一點???功能是..如何停止執行JavaScript間隔函數?
function autoLoad(chatboxtitle, msgId) {
setInterval(function() {
if (msgId != '') {
$.ajax({
type: "POST",
url: "../WebService1.asmx/GetMessagesDetail",
data: '{ "messageID": "' + msgId + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg) {
$("#chatbox_" + chatboxtitle + ".chatboxcontent").empty();
$("#chatbox_" + chatboxtitle + ".chatboxcontent").append(msg.d);
$contentLoadTriggered = false;
},
error: function(x, e) {
alert("error");
}
});
}
}, 8000);
}
將'setInterval()'的返回值存儲到變量中,並調用'clearInterval()'。 – 2012-07-11 13:49:21
除了Michael所說的,你是否考慮閱讀一些['setInterval()'文檔](https://developer.mozilla.org/en/DOM/window.setInterval)? – nnnnnn 2012-07-11 13:51:34