Alex
Alex

Reputation: 9265

Javascript global variables set modified within a function

I've got a a couple of functions similar to the following that will return an error should one occur:

$(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();
            }
        },
    });
}

else where in the code at the end of the script I set a refresh interval: var refInterval = window.setInterval('call()', 10000);

I want to be able to stop the refresh interval should an error occur. I've tried doing it by defining sort of a global variable window.hasErrors = false and setting it to true in the if else statement of the function and then seeing if it is false when the set interval is called but that didn't work.

Is there any way of accomplishing this?

Upvotes: 0

Views: 50

Answers (1)

coding_idiot
coding_idiot

Reputation: 13734

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

Some error handler

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

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

Upvotes: 1

Related Questions