javascript 用于 $timeout 的 Angular Js 回调函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14944121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular Js Callback Function For $timeout
提问by kamaci
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:
PS:问题在于:
$scope.sync.releasePrivilege();
$scope.sync.releasePrivilege();
is not running after timeout, it immediately runs.
超时后不运行,它立即运行。
回答by Josh David Miller
$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
是setTimeout
在测试期间被嘲笑的包装器。@MarkRajcok 关于为什么将其用作阻塞方法不起作用的说法是完全正确的。Mark 的解决方案也可以解决您的问题。但是,如果重新定位您的代码不可行,那么仍然有好消息!
$timeout
returns a promise (see $q
), so you can actually just chain together what you want:
$timeout
返回一个 promise(请参阅$q
),因此您实际上可以将您想要的内容链接在一起:
$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.
如果您愿意,这应该可以正常工作。它仍然没有阻止。它只是确保then
调用中的函数仅在 promise 被解决后才执行,即只有在超时完成并且您的方法已被调用时。
Additionally, your function can return data, if needed. So if tempFunc
returned a Boolean value that indicated success, you could also access it:
此外,如果需要,您的函数可以返回数据。因此,如果tempFunc
返回一个指示成功的布尔值,您还可以访问它:
$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!
请注意:在浏览器中休眠会非常糟糕 - 它会锁定 UI。异步执行使网络成为一个很棒的平台!
回答by Mark Rajcok
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.
超时不提供等效的“睡眠”。$timeout 将工作(在您的情况下,tempFunc)放在本机事件队列上,因此 tempFunc 将在稍后(在浏览器呈现之后)被调用。 $scope.sync.releasePrivilege();
因此将在 tempFunc 之前执行。正如您所说,如果您希望在 tempFunc() 之后执行 releasePrivilege(),请让 tempFunc() 调用它。