Can I use $ interval in a service?

Short version: reply to the title. Thank you for your help.

The longer version: I started, because, as I believe, many n00bs do, with a little code, added a little, a bit, and ended up with everything in one huge controller.

So, I shared my functionality and had a bunch of smaller controllers.

Then I wanted them to communicate with each other, and I discovered services.

Then I read that the controllers should be lean and medium, and I started moving the logic from the controllers to the services.

Now I find some kind of old code that is being read $scope.internetConnectionRetryTimer = $interval($scope.attemptInternetConnection, RECONNECT_ATTEMPT_FREQUENCY);

when moving to a service this.internetConnectionRetryTimer = $interval(this.attemptInternetConnection, RECONNECT_ATTEMPT_FREQUENCY); , the timer does not seem to work; either this or does not call the function upon expiration.

The same question as the short version: can I use $ interval in the service?


[Refresh] here is the code:

global vars SERVER - URL and var RECONNECT_ATTEMPT_FREQUENCY = 5 * 1000; // 5 seconds

   this.attemptInternetConnection = function()
   {
        $interval.cancel(this.internetConnectionRetryTimer);

        var params = '?action=test_connection&user=dummy';  

        $http.get(SERVER + params).  
            success(function() 
                {
                    $interval.cancel(this.internetConnectionRetryTimer); 
                    $rootScope.$broadcast('internetIsAvailable');
                })
               .error(function(status) 
               { 
                    this.internetConnectionRetryTimer = $interval(this.attemptInternetConnection, RECONNECT_ATTEMPT_FREQUENCY);  
                    $rootScope.$broadcast('internetIsUnavailable');
               });                         
   };// attemptInternetConnection()
+4
source share
2 answers

No problem with that. Here is an example:

<div ng-app="myApp" ng-controller="myCtrl">{{Data.Test}}</div>

angular.module('myApp', []).
controller('myCtrl', function ($scope, myService) {
    $scope.Data = {Test: 'Test'};
    myService.ChangeTest($scope.Data);
}).
service('myService', function ($interval) {
    this.ChangeTest = function (data) {
        $interval(function () {
            if (data.Test == 'Test') data.Test = 'Changed Test';
            else data.Test = 'Test';
        },500);
    }
});

Here is the fiddle .

+5
source

This should work fine. Although this depends on how the method was written attemptInternetConnection, as the code was not sent. If you reference any variables specific to the service inside attemptInternetConnection, you should refer to it by reference to the service object, such as the sample below.

Demo: http://plnkr.co/edit/1J0qzw044WRHSFGvZyOD?p=preview

app.service('intervalTest', function($interval) {
  var me = this;
  me.comments = [{
    total: 3,
    comment: 'some comment 1'
  }, {
    total: 10,
    comment: 'some other comment'
  }];
  this.getComments = function() {
    return me.comments;
  };
  $interval(function() {
    console.log('interval executed');
    me.comments[0].total++;
  }, 1000);
});
+1
source

Source: https://habr.com/ru/post/1543328/


All Articles