I have tried very hard to tell a RxJS timer to move onto the next sequence to no avail. Could someone please help me out with this one. I have busted my wits on how to do this. But have not managed it. Here is a sample of code explaining what I am trying to do. I have tried Subjects. I have No matter what I try it seems that you have to somehow throw the subscription away and restart, Even then how?? My comments in the code below should explain what I wish to achieve.
import { Component, OnInit } from '@angular/core';
import { timer } from 'rxjs';
import { takeWhile } from 'rxjs/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'testAngular4';
private condition = false;
ngOnInit(): void {
timer(0, 15000).pipe(
takeWhile((val) => {
console.log('inside takeWhile', ' val is ', val);
return val < 30000;
})).subscribe(value1 => {
console.log(' subject subscription value is ', value1);
timer(300, 3000).subscribe((value2) => {
// Do something here
console.log(' inside inner timmer subscribe and value is ', value2);
// How to tell the above timer to emit next value before the 3 seconds
// have elapsed. This would obviously be based on some condition
if (this.condition) {
// Emit the next value in the inner timer sequence above.
// I mean don't wait any longer - move on. Emit
}
});
// How to tell the above timer to emit next value before the 3 seconds
// have elapsed. This would obviously be based on some condition
if (this.condition) {
// Emit the next value in the outer timer sequence above
// I mean don't wait any longer - move on. Emit
}
});
}
}
If I understood your problem correctly, you want to emit an event after certain time interval in RxJS, you can evaluate throttleTime.
You can find a similar example in their documentation: https://rxjs-dev.firebaseapp.com/guide/overview
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.