angularjs $timeout without delay parameters reason
Asked Answered
M

1

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?

Momentum answered 16/12, 2013 at 12:0 Comment(0)
B
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.

Bifrost answered 16/12, 2013 at 12:28 Comment(2)
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.Lodovico
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.Ammonic

© 2022 - 2024 — McMap. All rights reserved.