将最小持续时间设置为加载栏

set mininum duration to loading bar

我有以下内容:

isLoading$: Rx.BehaviorSubject<boolean> = new Rx.BehaviorSubject(false);

 private getSomething(): void {
 this.isLoading$.next(true);
  this.http._get().subscribe((data) => {

//actions..

        },
            error => { console.log(error); },
            () => {
                 setTimeout(500);
                this.isLoading$.next(false);
            }

        );

}

我希望显示加载栏至少 500 毫秒才能看到它,因为大多数情况下数据检索速度如此之快以至于无法看到加载栏。

setTimeout 应该是这样的:

setTimeout(() =>{
  // some code here...
  this.isLoading$.next(false);
}, 500); 

您需要使用 setTimeout 调用来实现此目的 -

setTimeout(() => {
        this.isLoading$.next(false);
        // DO what else needed here
   }, 500);

您可以使用 timer and combine it with your http call using combineLatest。这将保证计时器中设置的最短时间,但如果 http 调用已经达到最短延迟,则不会再增加任何延迟。

import { timer } from 'rxjs/observable/timer';
import { combineLatest } from 'rxjs/observable/combineLatest';

getSometing() {
    const timerSource = timer(500);
    const httpSource = this.http._get();
    const httpWithMinTimer = combineLatest(timerSource, httpSource, (t, h) => h)

   httpWithMinTimer.subscribe(data => {this.isLoading$.next(false);})
}