【发布时间】:2016-09-22 06:31:35
【问题描述】:
如果承诺在五秒后没有返回,我有以下代码显示一条消息:
$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)
【问题讨论】:
标签: javascript angularjs timeout promise