Sorry for the confusing title, I really don't how how to phrase this question. I've tried googling a ton of different phrases but the same results keep coming up which isn't the what I'm looking for.
First of, I'm new to angularjs and javascript so apologies if I miss something pretty simple.
Looking at the interval documentation: https://docs.angularjs.org/api/ng/service/$interval
What I'm trying to do is call the $interval service like the following:
controller.js:
(function() {
function controller($scope, $interval, myService) {
var stop;
function handleCallback( myParams ) {
//do stuff with myParams
//call stopInterval function if neccessary.
}
$scope.doSomething = function() {
stop = $interval(myService.doWork, 5000, 0, handleCallback);
};
var stopInterval = function() {
if (angular.isDefined( stop ) ) {
$interval.cancel( stop );
stop = undefined;
};
};
}
angular.module( 'myApp' ).controller( 'controller', controller);
})();
my-service.js
(function() {
function myService($q, $http) {
myService.doWork = function( callback ) {
var dfd = $q.defer();
$http.get('url').success( function( response ) {
//Would parse the response into an appropriate response
//before calling the callback method with it.
callback( response );
dfd.resolve( response );
}).error( function( response ) ) {
//will add an error callback once I get this working
dfd.reject( response );
});
return dfd.promise;
}
return myService
}
angular.module( 'myApp' ).factory( 'myService', myService);
})();
When I step through this, and I start hitting the myService.doWork( callback ) method, callback is never a function but just a number and every time it's hit, the number is incremented by one.
I'm not sure where that number is coming from but I'm guessing I'm not setting up the $interval call properly. Looking at the angular documentation, the Pass param type says "*" so I'm assuming it means any type is supported and that functions are valid params. Is this not the case? Is it not possible to pass in a function as a param to fn for the $interval service?
-Thanks in advance for taking a look.