简体   繁体   中英

How can I stop execution of JavaScript interval function?

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);
}

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, ie 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM