延迟 inside/after 订阅 angular

Delay inside/after subscribe in angular

我在订阅中将 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 应该在一段时间内为 false

有更好的方法来显示弹出窗口。

但要回答你,一个干净的方法可能是:

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>