简体   繁体   中英

How to change interval dynamically with Observable pipe?

Here is a method. intervalValue is updated with a button click. The value is changing frequently.

intervalValue: number; 
intervalValue: Observable<number>; // also tried this. but interval method is looking for a number 

  ngOnInit() {

    interval(intervalValue)
    .pipe( 
              debounceTime(150),
              distinctUntilChanged(),
              tap(() => { 
                    ... 
                }), 
              startWith({}),
        switchMap(() => { 
         ... 
        }),
        map(data => {
          ... 
          return data.content;
        }),
        catchError(() => {
         ... 
        })
        ).subscribe(data => this.data = data);
}                                                       

But when the intervalValue changes, the interval still at it's initial value. How can I make interval change when the value of intervalValue changes?

You will need to start with Intervalue stream

const intervalValue=of(Math.random()*10000)
intervalValue
    .pipe( 
              switchMap(time=>interval(time))
              debounceTime(150),
              distinctUntilChanged(),
              tap(() => { 
                    ... 
                }), 
              startWith({}),
        switchMap(() => { 
         ... 
        }),
        map(data => {
          ... 
          return data.content;
        }),
        catchError(() => {
         ... 
        })
        ).subscribe(data => this.data = data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM