0

I ran into this blog post and author have used some code like this:

$timeout(function () {
    $timeout(function () {
        // some code
    }, 0);
}, 0);

I was wondering what this code really is doing?

Mistalis
  • 17,793
  • 13
  • 73
  • 97
Rathma
  • 1,196
  • 2
  • 33
  • 65
  • Check out the answers here http://stackoverflow.com/q/779379/87972 – Hoa Mar 28 '17 at 07:11
  • @Hoa I don't have any problem with the case when we have a single `$tiemout`, my question is about two nested timeoutes – Rathma Mar 28 '17 at 07:19
  • 2
    This is from the blog post you mentioned: "Getting the position, offset, height, width of the newly created elements are not guaranteed to provide the correct values, because the browser had not enough time to render and layout those elements. The hack is to further delay these operations. ". The author used the same technique addressed in the link I provided. – Hoa Mar 28 '17 at 07:21
  • 3
    This is really crappy, never seen anything like this in an Angular code. I have doubts about the author of this blog. – Mistalis Mar 28 '17 at 07:54
  • The blog author's assertion that the code will run after templateUrl has been loaded is incorrect. It will probably work if the template has been cached, but it will likely run prematurely if the the template needs to be loaded from a slow server. It is best to avoid using `$timeout` to fix flakey code. Look for a deterministic, reliable, robust solution. – georgeawg Mar 28 '17 at 17:51

0 Answers0