Poorman
Poorman

Reputation: 205

Update Observable timer in Angular

I would like to change dynamically the Observable timer setting.

    this.speed = 1000;
    let timer = Observable.timer(1, this.speed);

    sub = timer.subscribe(t => {
        if (t == 10) {
            this.speed = 500;
        }
    });

If for example, after X seconds, I set the this.speed = 500.

The example above doesn't work. Is it possible, or I have to unsubscribe this one and create a new one?

Upvotes: 1

Views: 1113

Answers (2)

user4676340
user4676340

Reputation:

You could try this :

createDelayedObservable(delay: number): Observable<any> {
  return Observable.of('value').delay(delay);
}

sub  = createDelayedObservable(1000).subscribe(res => {/* ... */ });

Upvotes: 1

Suren Srapyan
Suren Srapyan

Reputation: 68655

Observable is currently created with the given time. You can't change that given time to affect the observable. So you need to unsubscribe and create another one.

Upvotes: 1

Related Questions