setInterval in IONIC 3 or Ionic 4 :: typeScript

0
1808

There are Basically two methods to perform that.

Try using observable which will suit best your requirement.

Method 1:

import {Observable} from 'Rxjs/rx';
import { Subscription } from "rxjs/Subscription";

// if you want your code to work everytime even though you leave the page
Observable.interval(1000).subscribe(()=>{
    this.functionYouWantToCall();
});

Method 2:

// if you want your code to work only for this page
//define this before constructor
observableVar: Subscription;

this.observableVar = Observable.interval(1000).subscribe(()=>{
    this.functionYouWantToCall();
});

ionViewDidLeave(){
   this.observableVar.unsubscribe();
}

Method 3:


 reLoad:any;
    ngOnInit() {
      this.reLoad =  setInterval( ()=>{
        console.log('*** Set Interval ***');
        this.getOutletDetail();
      },10000);
      
    }
    
    ngOnDestroy()
    {
      clearInterval(this.reLoad);
    }

LEAVE A REPLY

Please enter your comment!
Please enter your name here