clearInterval()不起作用

时间:2015-07-23 08:28:09

标签: javascript java ajax spring

<script>
var interval_time;

function executeQuery() {
    $.ajax({
        method: 'GET',
        url: "${pageContext.request.contextPath}/get-session-timeout",
        success: function(jsonRes, textStatus, jqXmlHttpRequest) {
            console.log('success log session out');
            console.log(jqXmlHttpRequest.status);
        }

    });
    interval_time = setInterval(executeQuery, 30000);
}

function stopInterval() {
    console.log('stop interval hit');
    clearInterval(interval_time);
}
</script>
<script>
$(document).ready(function(e) {
    console.log('ready');
    interval_time = setInterval(executeQuery, 30000);

});
</script>



//FILE UPLOAD AJAX CALL

function uploadJqueryForm() {
    //logic
    $uploadFrm.ajaxForm({
        beforeSend: function() {
            interval_time = setInterval(executeQuery, 30000);
            //logic
        },
        success: function(jsonRes) {
            stopInterval();
            //logic
        },
        error: function(xhr, textStatus,
            errorThrown) {
            stopInterval();
            //logic
        },
        complete: function(xhr, extStatus, errorThrown) {
            stopInterval();
        },
        dataType: "text"
    }).submit();
}
return false;
}

2 个答案:

答案 0 :(得分:1)

您多次覆盖interval_time。然后,当您拨打stopInterval()时,interval_time中的最后一个时间间隔将停止,而不是其他时间间隔。

答案 1 :(得分:1)

如果您在已经拥有它的变量中使用setInterval()而不停止它,您将无法清除它。因此,您应该使用clearInterval(),然后使用setInterval()