我在订阅中将isUpdate
标志设置为true,并且必须经过一段时间延迟才能将其设置为false,这样我才能显示快速弹出窗口
我尝试使用.pipe(delay(2000)).subscribe
,但是整个回调都被延迟了
this.sp.myservice(data).subscribe(
data => {
this.isUpdate = true;
//something like this but not setTimeout
setTimeout(() =>
{
this.isUpdate = false
}, 2000)
}
);
预期结果:isUpdated在某些时候应该为假
答案 0 :(得分:1)
有更好的显示弹出窗口的方法。
但是要回答您,一个干净的方法可能是:
this.sp.myservice(data).pipe(
tap(() => this.isUpdate = true),
delay(5000),
).subscribe(() => this.isUpdate = false);
实时模式:
rxjs.of('some mock value').pipe(
rxjs.operators.tap(() => console.log('Wait 5 seconds')),
rxjs.operators.delay(5000),
).subscribe(() => console.log('See ? I am delayed.'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.js"></script>
根据您的请求:
this.sp.myservice(data).pipe(
tap(data => this.isUpdate = this.data && this.data.status && this.data.status.code === 0),
delay(5000),
catchError(err => throwError(err))
).subscribe(
() => this.isUpdate = false,
err => console.log('an error occured')
);
实时模式:
rxjs.throwError('some error mock value').pipe(
rxjs.operators.tap(() => console.log('Wait 5 seconds')),
rxjs.operators.delay(5000),
rxjs.operators.catchError(err => rxjs.throwError(err))
).subscribe(
() => console.log('See ? I am delayed.'),
err => console.log('an error occured')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.js"></script>