JavaScript: What would cause setInterval to stop firing?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-04-09T00:48:28Z Indexed on 2010/04/09 0:53 UTC
Read the original article Hit count: 304

Filed under:
|

I am writing a chat AJAX app. Randomly, in FF 3.5.9, setInterval() seems to stop firing. I don't have clearInterval() anywhere in my code. What could be causing this to happen?

$(document).ready(function () {

    $("#no-js-warning").empty();

    messageRefresher = new MessageRefresher(0);
    setInterval($.proxy(messageRefresher, "refresh"), 2000);
});

function notifyInRoom(user) {
    $.getJSON('API/users_in_room', { room_id: $.getUrlVar('key'), username: user }, function (users) {
        if (!$.isEmptyObject(users)) {
            $("#users").empty();
            $.each(users, function (index, username) {
                var newChild = sprintf("<li>%s</li>", username);
                $("#users").append(newChild);
            });
        }
        else {
            $("#users-loading-msg").text("No one is in this room.");
        }
    });
}   

function MessageRefresher(latest_mid) {
    this._latest_mid = latest_mid; 
}

MessageRefresher.prototype.refresh = function () {
    notifyInRoom($("#user-name").val());

    var refresher = this;
    $.getJSON('API/read_messages', { room_id: $.getUrlVar('key'), mid: refresher._latest_mid }, function (messages) {
        if (! (messages == null || $.isEmptyObject(messages[0]))) { // messages will always be at least [[], [], 0]
            $("#messages-loading-msg").hide();
            for (var i = 0; i < messages[0].length; i++) {
                var newChild = sprintf('<li><span class="username">%s:</span> %s</li>', messages[1][i], messages[0][i]);
                $("#messages").append(newChild);
            }
            refresher._latest_mid = messages[2];
            setUserBlockClass();
        }
        else {
            $("#messages-loading-msg").text("No messages here. Say anything...");
        }

    });
}

// Give the end-user-block class to the appropriate messages
// eg, those where the next message is by a different user  
function setUserBlockClass() {
    $("#messages li").each(function (index) {
        if ($(this).children(".username").text() != $(this).next().children(".username").text()) {
            $(this).addClass("end-user-block");
        }
    });
}

I checked the most recent responses in Firebug, and it was the same responses that had been sent earlier. (So, it's not like an unusual response caused a crash.)

If I refresh the page, the calls resume.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about AJAX