在AJAX JQuery中停止setInterval

时间:2014-11-17 10:39:40

标签: jquery ajax

对于那些比我更有经验的人提出的问题。如何停止执行具有此代码的setInterval并且无法使用main函数? 谢谢你的帮助。

$(".scrivi-messaggio").click(function()
{         
    var username_utente = $('#username-utente').val();    
    $('.modal-title').html('Chat di '+username_chat);

    setInterval(function()
    {

        $.get("//localhost/laravel/public/index.php/stampa_messaggi/"+id, 

        function(data)
        { 
            data_parsed = JSON.parse(data);

            var resultHtml = '';

            $.each(data_parsed, function(i, el) 
            {
                if(el.id_user === id_user)
                {
                    resultHtml += "<a href='http://localhost/laravel/public/index.php/utente/" + id_user + "' style='font-weight: bold;'>" + username + "</a> " + el.contenuto + "<br />";
                }
                else
                {
                    resultHtml += "<a href='http://localhost/laravel/public/index.php/utente/" + el.id_user + "' style='font-weight: bold;'>" + username_utente + "</a> " + el.contenuto + "<br />";
                }
            });

            $(".stampa-messaggi").html(resultHtml).show();

        });
    }, 2000);    
});

4 个答案:

答案 0 :(得分:2)

为您的间隔创建一个var:

var myInterval = setInterval(function(){/*...*/}, 2000);

然后使用clearInterval来阻止它:

clearInterval(myInterval);

单击按钮清除间隔:

$('#buttonID').on("click", function() {
    if (typeof myInterval != 'undefined') clearTimeout(myInterval);
});

答案 1 :(得分:1)

如果您的AJAX请求因任何原因失败或延长,setInterval()将继续发出新请求。

由于你经常发出一个AJAX请求,你应该正在做的是在回调函数中使用setTimeout(),这将仅在最后完成。您可以将代码放在函数中,并在每次超时时调用此函数。

您可以将超时存储在可访问范围内的变量中,然后单击按钮将其清除:

var myTimeout = undefined,
    getData = function () {
    $.get("//localhost/laravel/public/index.php/stampa_messaggi/" + id,
    function (data) {
        data_parsed = JSON.parse(data);

        var resultHtml = '';

        $.each(data_parsed, function (i, el) {
            if (el.id_user === id_user) {
                resultHtml += "<a href='http://localhost/laravel/public/index.php/utente/" + id_user + "' style='font-weight: bold;'>" + username + "</a> " + el.contenuto + "<br />";
            } else {
                resultHtml += "<a href='http://localhost/laravel/public/index.php/utente/" + el.id_user + "' style='font-weight: bold;'>" + username_utente + "</a> " + el.contenuto + "<br />";
            }
        });
        $(".stampa-messaggi").html(resultHtml).show();

        myTimeout = setTimeout(getData, 2000);
    });
}

$(".scrivi-messaggio").click(function () {
    var username_utente = $('#username-utente').val();
    $('.modal-title').html('Chat di ' + username_chat);
    getData();    
});

$('button').click(function(){
    if(typeof myTimeout != 'undefined') clearTimeout(myTimeout);
});

答案 2 :(得分:0)

setInterval返回
An integer with the ID value of the timer that is set. Use this value with the clearInterval() method to cancel the timer. ref

因此您可以使用clearInterval将此ID作为参数ref

传递

答案 3 :(得分:0)

setInterval()返回一些名为intervalId的描述符,可以将clearInterval' stop the interval associated with it! There for you should assign the return value of setInteval()`传递给变量。

var intervalId
//declare it outside the click scope so that you can access it later
$(".scrivi-messaggio").click(function()
{         
    var username_utente = $('#username-utente').val();    
    $('.modal-title').html('Chat di '+username_chat);

    intervalId = setInterval(function(){/*What ever the function does*/}, 2000);    
});

要稍后停止间隔,您可以使用clearInterval(intervalId) 希望这有帮助!