i have the following service that get some data from an API and it works fine
i call this service in the app.run() method
// some code...
$scope.pageStudents;
$scope.getStudentsPage = function(){
$http({
method: 'GET',
url: 'studentsResource/students'
}).then(function successCallback(response) {
$scope.pageStudents = response.data;
});
};
//i want to call this method every 5 min for example.
$scope.getStudentsPage();
my issue is how i can call my $scope.getStudentsPage();
after each 5 min , something like scheduled job (like quartz in java) , is angular or javascript support this concept ? please anyone can guide me on how to achieve this operation. thanks in advance.
If you want to take a delayed action once, then you can use angular's $timeout
service. If you want it to happen multiple times, there's the $interval
service.
So like this:
$scope.pageStudents;
$scope.getStudentsPage = function(){
$http({
method: 'GET',
url: 'studentsResource/students'
}).then(function successCallback(response) {
$scope.pageStudents = response.data;
});
};
$interval(function () {
$scope.getStudentsPage();
}, 300000); // <--- 5 minutes in milliseconds
In plain javascript, you would use window.setTimeout or window.setInterval to do this. In angularjs you should instead use the $timeout and $interval services, because angularjs won't be able to run its change detection correctly otherwise.
Documentation for $timeout and $interval can be found here:
https://docs.angularjs.org/api/ng/service/$timeout
https://docs.angularjs.org/api/ng/service/$interval
And for setTimeout and setInterval here:
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.