Skip to content

Instantly share code, notes, and snippets.

@rePassante
Last active December 18, 2015 00:19
Show Gist options
  • Save rePassante/5695763 to your computer and use it in GitHub Desktop.
Save rePassante/5695763 to your computer and use it in GitHub Desktop.
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