jquery - How can I stop execution of JavaScript interval function? - Stack Overflow

I have a function with in setIntervel(), and I want to stop the execution of this function explicitly.H

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 call clearInterval() 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
Add a ment  | 

1 Answer 1

Reset to default 10

If 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信