What advantage is there in using the $timeout in AngularJS instead of window.setTimeout?

后端 未结 3 1529
天涯浪人
天涯浪人 2020-11-27 15:11

I had a suggestion to implement a timeout like this:

  $timeout(function() {

    // Loadind done here - Show message for 3 more seconds.
    $timeout(functi         


        
3条回答
  •  日久生厌
    2020-11-27 16:05

    AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc.

    By using the AngularJS $timeout service, the wrapped setTimeout will be executed in the AngularJS execution context.

    For more information, see

    • AngularJS $timeout Service API Reference
    • AngularJS Developer Guide - Integration with the browser event loop

提交回复
热议问题