[英]show a message on timeout, for only two seconds
我有以下代碼,如果五秒鍾后沒有返回承諾,則會顯示一條消息:
$timeout(function () {
if (!$scope.promiseComplete && $scope.submitted) {
$scope.message = {
content: [{
title: '',
msg: 'Service down - created'
}],
type: 'error'
};
return;
}
}, 5000)
我的困境是我只想顯示此消息兩秒鍾。 我嘗試了以下操作,並且有效,這是防止“嵌套”超時的反模式嗎?
$timeout(function () {
if (!$scope.promiseComplete && $scope.submitted) {
$scope.message = {
content: [{
title: '',
msg: 'Service down - Railcar ASN created'
}],
type: 'error'
};
$timeout(function(){
$scope.message = null;
}, 2000)
return;
}
}, 5000)
更新:根據答案,這就是我的目的:
$timeout(function () {
if (!$scope.promiseComplete && $scope.submitted) {
$scope.message = {
content: [{
title: '',
msg: 'Service down - created'
}],
type: 'error'
};
return $timeout(function(){
$scope.message = null;
}, 2000)
}
}, 5000)
$timeout
基於promise,它返回可以鏈接的promise。 是的,嵌套$timeout
或其他任何承諾都可能是反模式 。
這種承諾鏈
$timeout(function () {
if (!$scope.promiseComplete && $scope.submitted) {
...
return $timeout(function(){
$scope.message = null;
}, 2000)
}
}, 5000)
保證最多嵌套2個級別,並且可以使用top $timeout
返回的承諾,可以擴展或測試整個承諾鏈。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.