Friday, November 14, 2014

implementing javascript continuous callback

trying to figure out how to implement Facebook's message badge notification

probably it's as simple as a function calling itself over and over again - javascript lends itself to this kind of style, taking advantage of the non-blocking attribute. Although this might not be the optimal way of doing it - probably setting a boolean variable inside the loop to deactivate/reactivate it would be a good idea (deactivate when user activity is not detected and just reactivate otherwise)

$scope.messageCount would later be interpolated within the badge elements

var monitor = function() {
    setTimeout(function() {
        console.log('monitoring ..');
        MessageFactory.getMessages('545846a622f1a2a188405579', function(messages) {
            $scope.messages = messages;
            $scope.messageCount = messages.length;
        });
        monitor();
    }, $scope.global.refreshInterval);
};
monitor();

or probably I'm better off with an infinite loop?

- Rain

No comments:

Post a Comment