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

后端 未结 3 1531
天涯浪人
天涯浪人 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 15:44

    1. It wraps your callback for you automatically in a try/catch block and let's you handle errors in the $exceptionHandler service: http://docs.angularjs.org/api/ng.$exceptionHandler
    2. It returns a promise and thus tends to interoperate better with other promise-based code than the traditional callback approach. When your callback returns, the value returned is used to resolved the promise.

提交回复
热议问题