Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Polling In AngularJS
$scope.startPolling = function () {
$scope.isPolling = true;
$scope.pollCount = 60;
$scope.keepPolling();
};
$scope.stopPolling = function () {
$scope.isPolling = false;
$scope.pollCount = 60;
};
$scope.keepPolling = function () {
if ($scope.isPolling) {
if ($scope.pollCount > 0) {
$scope.pollCount--;
$timeout($scope.getSomeData, 200);
} else {
$scope.stopPolling();
}
}
};
$scope.getSomeData = function () {
console.log('Fetch Some Data');
$scope.response = $http.get('api/MockData', { params: {},headers: {} }).success(
function (response) {
$scope.mockData = response;
$scope.keepPolling();
}).error(
function (error) {
console.error(error);
$scope.keepPolling();
}
);
};
};
$scope.isPolling = false;
$scope.pollCount = 60;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment