最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular Js Callback Function For $timeout - Stack Overflow

programmeradmin1浏览0评论

I have that line of codes

...
$timeout(tempFunc, $scope.sync.getDelay());
...

at my temp function I have that line of code at end:

$scope.sync.releasePrivilege();

and everything works well. However when I try:

...
$timeout(tempFunc, $scope.sync.getDelay());
$scope.sync.releasePrivilege();
...

It doesn't. I think that I should write that line as a callback function into timeout. I don't want to change recent functions at my code I can just edit that lines.

Any ideas?

PS: The problem is that:

$scope.sync.releasePrivilege();

is not running after timeout, it immediately runs.

I have that line of codes

...
$timeout(tempFunc, $scope.sync.getDelay());
...

at my temp function I have that line of code at end:

$scope.sync.releasePrivilege();

and everything works well. However when I try:

...
$timeout(tempFunc, $scope.sync.getDelay());
$scope.sync.releasePrivilege();
...

It doesn't. I think that I should write that line as a callback function into timeout. I don't want to change recent functions at my code I can just edit that lines.

Any ideas?

PS: The problem is that:

$scope.sync.releasePrivilege();

is not running after timeout, it immediately runs.

Share Improve this question edited Feb 18, 2013 at 20:21 kamaci asked Feb 18, 2013 at 19:46 kamacikamaci 75.2k72 gold badges243 silver badges371 bronze badges 2
  • What exact "doesn't work"? Can you post not working code to plnkr.co or jsfiddle to see what is wrong – Valentyn Shybanov Commented Feb 18, 2013 at 20:14
  • 1 Can you post plunker example that shows that is not been called after timeout? – Valentyn Shybanov Commented Feb 18, 2013 at 20:51
Add a comment  | 

2 Answers 2

Reset to default 14

$timeout is a wrapper for setTimeout that gets mocked out during testing. @MarkRajcok is completely right about about why using it as a blocking method doesn't work. Mark's solution would also solve your issue. But if it's not feasible to relocate your code, there is still good news!

$timeout returns a promise (see $q), so you can actually just chain together what you want:

$timeout( tempFunc, $scope.sync.getDelay() ).then( function() {
  console.log("I'm called only after the timeout.");
  $scope.sync.releasePrivilege();
});

console.log("But I get called immediately.");

And this should work just fine, should you fancy. It still doesn't block. It just ensures that the function within the then call is executed only after the promise is resolved, that is only when the timeout has completed and your method has been called.

Additionally, your function can return data, if needed. So if tempFunc returned a Boolean value that indicated success, you could also access it:

$timeout( tempFunc, $scope.sync.getDelay() ).then( function( result ) {
  if ( result ) {
    $scope.sync.releasePrivilege();
  } else {
    // handle the error
  }
});

And there was much rejoicing. Yay.


Just as a note: doing a sleep in a browser would be very bad - it'd lock the UI. Asynchronous execution is what makes the web an awesome platform!

Timeout does not provide the equivalent of a "sleep". $timeout puts work (in your case, tempFunc) on the native event queue, and hence tempFunc will be called later (after the browser renders). $scope.sync.releasePrivilege(); will therefore be executed before tempFunc. As you stated, if you want releasePrivilege() to execute after tempFunc(), have tempFunc() call it.

发布评论

评论列表(0)

  1. 暂无评论