如何停止执行JavaScript间隔功能?

ank*_*kit 1 javascript jquery setinterval

我在setIntervel()中有一个函数,我想明确地停止执行这个函数.我怎么能这样做???函数是..

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);
}
Run Code Online (Sandbox Code Playgroud)

Utk*_*nos 10

如果您的意思是退出该功能但不停止间隔(因此该功能将再次触发),您只需return要从该功能.

如果您的意思是停止间隔,即停止再次触发功能,则需要在变量中分配对间隔的引用,然后在/根据需要清除它.

var intie = setInterval(function() { alert('hello'); }, 5000);
clearInterval(intie);
Run Code Online (Sandbox Code Playgroud)