0

I have 2 controllers, both use the same update method to get data:

angular.module('project')
  .controller('mainController', function($http, $scope, $timeout, $sce) {
      updateData($http, $scope, $timeout, $sce, false);
  })

  .controller('settingsController', function($http, $scope, $timeout, $sce) {

      updateData($http, $scope, $timeout, $sce, true);          
  })

my updateData looks like this:

function updateData($http, $scope, $timeout, $sce, settings) {
    $timeout(function() {
        if (settings) {

            getSettings($http, $scope);
        }
        else {
            getDataA($http, $scope);
            getDataB($http, $scope);
        }
        updateData($http, $scope, $timeout, $sce, settings);
    }, 1000);
}

Now when refreshing the main page (using mainController) I always get 'null' messages, because the process did not complete all method calls, and when switching to another site (using settingsController) it takes ages to load it because all requests from the previous one have to be completed first. How do I directly "kill" all pending updates when refreshing/switching site?

1
  • Its incorrect implementation..passing dependency as parameter to method isn't correct.. rather method should be there inside a service. so that we can ask for data from them.. Commented Mar 24, 2016 at 10:06

2 Answers 2

3
  1. This implementation is wrong. I don't think it's a good idea to pass a dependency to an open function and use them. This can create hard-to-detect bugs.

  2. Your updateData function is a resource killer. It recurs every second without a limitation or control.

There is a design issue in your code. You should reconsider designing it in a standard and effective way.

The answer to your question is that $timeout returns a promise. You can use the $timeout.cancel method to cancel the timeout.

var timeoutPromise = $timeout(foo(), 1000);

$timeour.cancel(timoeoutPromise); 
Sign up to request clarification or add additional context in comments.

Comments

0

you should make a service instead of the function. then you need to inject the service into the controllers.

project.service('updateService', function(){
    ...some logic here...
});

then inject:

angular.module('project')
 .controller('mainController', function($http, $scope, $timeout, $sce, updateService) {
        ...use "updateService" methods here...
})

and when you write a controller, a small tip, write it like this:

angular.module('project')
 .controller('someController', ['$scope', function($scope){

}];

this is important for when you want to make minified version of your code.

5 Comments

so I put 2 methods (updateData and updateSettings) in a service, call one in each controller, which then automatically stops being executed when the controller changes/refreshes?
@JustSomeDude , yes and this is the right thing to do. it is safer and easy to debug. i hope you liked my answer. if i can help in any other way please ask.
I just checked services and I think I'm still confused, it seems to me like a service is actually just a function, so do I create two services now? One for each controller (e.g. updateSettingsService and updateDataService)?
think of a service as a placeholder for a logic (or atlist this is what i am doing), that several controller need to inherit. you can make two service and inject both it is totally fine. if you liked my answer please accept it.
in my getDataA, getDataB methods I set the data directly in $scope, which is why I passed it thorugh all methods, also I call updateDate in updateData itself so it will be executed every second... would you mind showing me how the service would look like in my case, I cant figure it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.