4

I am trying to read some angularjs code There is a point where the $timeout is been called with no delay parameter.

dataBinding: () => {
            this.$timeout(() => {
                this.selectedRow = null;
            });
        },

What is the purpose of that ?

user3814030
  • 1,263
  • 3
  • 20
  • 37
  • **unclear what you're asking** Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – Vivek Dec 17 '15 at 11:28
  • To make sure the code runs asynchronously. – Johannes Jander Dec 17 '15 at 11:34

2 Answers2

5

In this context (angular.js), this is a workaround - when you simply want to defer your action to the next angular digest cycle (and to be sure that is not happening in the current digest cycle).

If this is your case, you'd better use $scope.$evalAsync() for this purpose. See http://www.bennadel.com/blog/2605-scope-evalasync-vs-timeout-in-angularjs.htm

Ioan
  • 5,152
  • 3
  • 31
  • 50
  • Also `$timeout` will return a promise. It is feasible that it could be used to replace `$q.when` if for some reason the dev didn't want to inject `$q`. – ste2425 Dec 17 '15 at 11:53
  • True, but in this case no one is doing nothing with the result got by calling `$timeout`. – Ioan Dec 17 '15 at 11:54
  • 2
    Yup, just adding for completeness. – ste2425 Dec 17 '15 at 11:55
1

If you don't set delay value then it is set to 0.

But the actual delay may be longer; see this

And you can see why setting delay value to 0 is useful?

Community
  • 1
  • 1
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231