Here I have query that how can I call a interval method in the subscribe and stop it after the condition is met
below is my code
Main Subscribe method
this.authservice.getdata().subscribe(response => {
console.log(res)
// Polling method
SetInterval(()=>{
this.getInterval();
},5000)
})
Interval Method:
getInterval(){
this.authservice.getIntervalData().subscribe(resp => {
console.log(resp)
this.responseStatus = resp
})
}
In the response it gives 3 types of response
Proceed
Stop
Terminate
So here I need to call that getInterval() in Main subscribe method and until GetInterval gives me Proceed as response or until 1 min from polling I have set the interval running if either of this conditions are met i have to stop polling.
Note: I dont need polling for Main Subscribe method if main subscribe give response on success then only I have starte this polling
Update: The below Method works but here i need 2 things
How can set variables and store the response in that
How can i set the boolean variable based on res like if the response comes
this.content =true like wise
because when i am trying to set the variables in switchMap it is not accepting
this.authservice.getdata().pipe( switchMap(resp => timer(0, 5000).pipe( // need to store this response in global variable so that it can be used for further switchMap(t => this.authservice.getIntervalData( <1st response param> ).pipe( map(data => [data, t]))) takeWhile(([data, t]) => data !== 'TERMINATE' && t < 12), map(([data, t]) => data) )) ).subscribe(data => console.log(data))
you probably want to just use operators and observables....
this.authservice.getdata().pipe( // get initial data
tap(resp => this.myVariable = resp),
switchMap(resp => timer(0, 5000).pipe( // switch to timer that emits every 5 seconds
switchMap(t => this.authservice.getIntervalData().pipe( // swtich again to interval data
map(data => [data, t]))) // map to response and timer value
takeWhile(([data, t]) => data !== 'TERMINATE' && t < 12), // whatever stop condition, this says stop when the data is terminate or the timer has emitted 12 times (1 minute if emitting every 5 seconds)
map(([data, t]) => data) // just send the data through
))
).subscribe(data => console.log(data))
something like that is how I would implement this.