Last active
December 18, 2015 00:19
-
-
Save rePassante/5695763 to your computer and use it in GitHub Desktop.
Polling In AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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