33

In a few script I can find for instance

$timeout(function () {
    $scope.my = 1;            
});

instead of simply

$scope.my = 1;

What's the purpose to call $timeout without delay?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Whisher
  • 31,320
  • 32
  • 120
  • 201

1 Answers1

54

This is a hack. :) But usually the intention is to wait until the end of the $digest cycle and then set $scope.my to 1. Timeouts are called after all watches are done.

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • 3
    Davin, could you explain WHY it is a hack? Maybe provide an example of when it might be used and what the ACTUAL IDEAL solution would be? I notice it is quite prevalent in a project at work, and I understand what it fixes, but I can't quite figure out what a better solution would be. – karns Sep 20 '16 at 21:18
  • Exemple : I have a click event which trigger an apply on the current scope and I have a watcher which triggers the same click event. I had to make a condition to not reload the apply call into the event and a timeout into the watcher to wait the end of the digest cycle (due to apply() called in the event). I'll post the code when soon. – Disfigure Jan 13 '17 at 13:24