0

isUpdate在订阅中设置标志为真,我必须在延迟一段时间后将其设置回假,以便我可以显示一个快速弹出窗口

我尝试过使用.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 在一段时间内应该为假

4

1 回答 1

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>

于 2019-10-03T13:55:09.380 回答