显示超时消息,仅两秒钟
show a message on timeout, for only two seconds
我有以下代码,如果 promise 在五秒后 return 没有显示消息:
$timeout(function () {
if (!$scope.promiseComplete && $scope.submitted) {
$scope.message = {
content: [{
title: '',
msg: 'Service down - created'
}],
type: 'error'
};
return;
}
}, 5000)
我的困境是我只想显示此消息本身两秒钟。我尝试了以下方法并且有效,它是 "nest" 超时的反模式吗?
$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
是基于承诺的,它 returns 是一个可以链接的承诺。是的,嵌套 $timeout
或任何其他承诺 may be an antipattern。
这种承诺链
$timeout(function () {
if (!$scope.promiseComplete && $scope.submitted) {
...
return $timeout(function(){
$scope.message = null;
}, 2000)
}
}, 5000)
保证嵌套不超过2层,top$timeout
返回的promise可以利用,整个promise链可以扩展或测试。
我有以下代码,如果 promise 在五秒后 return 没有显示消息:
$timeout(function () {
if (!$scope.promiseComplete && $scope.submitted) {
$scope.message = {
content: [{
title: '',
msg: 'Service down - created'
}],
type: 'error'
};
return;
}
}, 5000)
我的困境是我只想显示此消息本身两秒钟。我尝试了以下方法并且有效,它是 "nest" 超时的反模式吗?
$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
是基于承诺的,它 returns 是一个可以链接的承诺。是的,嵌套 $timeout
或任何其他承诺 may be an antipattern。
这种承诺链
$timeout(function () {
if (!$scope.promiseComplete && $scope.submitted) {
...
return $timeout(function(){
$scope.message = null;
}, 2000)
}
}, 5000)
保证嵌套不超过2层,top$timeout
返回的promise可以利用,整个promise链可以扩展或测试。