如何停止setInterval();?

-4 javascript

我喜欢......

<div id="div"></div>
<script src="jquery.js"></script>
<script src="updater.js"></script>
Run Code Online (Sandbox Code Playgroud)

updater我想从我的.php文件中获取数据,所以可能就像...

setInterval(function() {
    $.ajax({
        type: "GET",
        url: "test.php",
        success: function(html) {
             // html is a string of all output of the server script.
            $("#div").html(html);
       }
    });
}, 5000);
Run Code Online (Sandbox Code Playgroud)

然后,显然足够了test.php的页面.

但是,我想停止setInterval(); 当有人点击评论按钮(textarea)时.我知道,这里有一些; 但他们没有工作?

Sat*_*pal 5

Window.clearInterval,它接受a intervalID是您要取消的重复操作的标识符.此ID是从setInterval().

var interVal = setInterval(function() {
    $.ajax({
        type: "GET",
        url: "test.php",
        success: function(html) {
             // html is a string of all output of the server script.
            $("#div").html(html);
       }
    });
}, 5000);

clearInterval(interVal); //Call the method when you want to clear it.
Run Code Online (Sandbox Code Playgroud)