AngularJS – Polling for data

Polling for data in AngularJS can be accomplished quickly using the $timeout function. You pass two parameters to the $timeout instance; The first being the method to call, the second being the delay (in milliseconds).

$timeout($scope.getSomeData, 200);

Combining this with the counter enables you to quickly wire-up a repeated call for data over a specified timeframe.

$scope.keepPolling = function () {
    if ($scope.isPolling) {
        if ($scope.pollCount > 0) {
            $scope.pollCount--;
            $timeout($scope.getSomeData, 200);
        } else {
            $scope.stopPolling();
        }
    }
};

A full example can be seen in the following Gist

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *