[英]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.