I have an API on q server that looks like this:
function isLoggedIn(){
....
}
This API tell whether a user is logged in or not.
I want to call this API every 15 minutes or within any interval, to check whether the user is authenticated or not on my angular app.
My concern is how to call an API in intervals, and where to write this method.
Use setInterval in your main controller.
setInterval(function(){
// call your service method here
//isLoggedIn(); in your case
}, 3000); // This is time period in milliseconds 1000 ms = 1 second.
Or
use $interval service is injected into your controller function, you can use it to schedule repeated function calls. Here is an example that used the $interval service to schedule a function call every 5 seconds:
var myapp = angular.module("myapp", []);
myapp.controller("MyController", function($scope, $interval){
$interval(isLoggedIn, 5000);
});
function isLoggedIn() {
console.log("Interval occurred");
// call your service method here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With