在函数中修改的Javascript全局变量集

时间:2017-01-31 22:20:08

标签: javascript jquery error-handling

我有一些类似于以下的功能会在发生错误时返回错误:

$(document).ready(function() {
    call();
});
function call() {
    getBounceRate();
    getVisitsToday();
    getMom();
}
function getVisitsToday() {
    $('#loader-visits').show();
    $('#visits-today').closest('#show').hide();
    $('#visits-today').next().remove();
    $.ajax({
        url: 'framework/AJAX/stats_data.php',
        type: 'GET',
        data: 'type=visitsToday',
        success: function(data) {
            if (!$.isNumeric(data)) {
                data = $.parseJSON(data);
                $('#visits-today').parent().show().html(data.error);
                $('#loader-visits').hide();
               // trigger error so refresh doesn't happen

            } else {
                $('#visits-today').html(data);
                $('#visits-today').after('<span class="text-muted">unique visitors</span>');
                $('#loader-visits').hide();
                $('#visits-today').closest('#show').fadeIn();
            }
        },
    });
}

否则在脚本末尾的代码中我设置了一个刷新间隔:var refInterval = window.setInterval('call()', 10000);

我希望能够在发生错误时停止刷新间隔。我尝试通过定义全局变量window.hasErrors = false的排序并在函数的if else语句中将其设置为true,然后在调用设置的时间间隔时查看它是否为false但是没有&# 39;工作。

有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:1)

var ii = window.setInterval('call()', 10000);

一些错误处理程序

function errorHandler(){
   window.clearInterval(ii);
}

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval

相关问题