I have a function with in setIntervel(), and I want to stop the execution of this function explicitly.How I can do this ???The function is..
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);
}
I have a function with in setIntervel(), and I want to stop the execution of this function explicitly.How I can do this ???The function is..
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);
}
Share
Improve this question
edited Jul 11, 2012 at 13:56
Pointy
414k62 gold badges595 silver badges629 bronze badges
asked Jul 11, 2012 at 13:47
ankitankit
391 silver badge9 bronze badges
2
-
3
Store the return of
setInterval()
into a variable, and callclearInterval()
on it. – Michael Berkowski Commented Jul 11, 2012 at 13:49 -
1
Further to what Michael said, did you consider reading some
setInterval()
documentation? – nnnnnn Commented Jul 11, 2012 at 13:51
1 Answer
Reset to default 10If you mean exit the function but not stop the interval (so the function will fire again) you need to simply return
from the function.
If you mean stop the interval, i.e. stop the function ever firing again, you need to assign a reference to the interval in a variable and then clear it when/as required.
var intie = setInterval(function() { alert('hello'); }, 5000);
clearInterval(intie);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745302582a4621526.html
评论列表(0条)