We have used setInterval() to run a task multiple times after every specific interval. We need to clear interval after the task is done. Using RxJs interval function we can simulate setInterval function.
import { interval } from "rxjs";
// creating observable
let numbers = interval(1000);
// subscribe function returns reference to observable
let numbesubs = numbers.subscribe(x => {
console.log("Next: ", x);
// clearing interval when x reaches 10 value
if (x == 10) {
unSubscribingfunction();
}
});
function unSubscribingfunction() {
// clearing interval
numbesubs.unsubscribe();
}
// Logs:
// Next: 0
// Next: 1
// Next: 2
// ..
// Next: 10
Top comments (0)