简体   繁体   English

clearInterval()不起作用

[英]clearInterval() not working

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

You override interval_time many times. 您多次覆盖interval_time Then when you call stopInterval() only the last interval in interval_time will be stopped not the others. 然后,当您调用stopInterval()只会停止interval_time中的最后一个间隔,而不会停止其他interval_time

If you use setInterval() in a variable which already has it without stoping it, you won't be able to clear it. 如果在不停止运行的变量中使用setInterval() ,则将无法清除它。 So you should use clearInterval() and then setInterval() after it. 因此,您应该使用clearInterval() ,然后再使用setInterval()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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