0

How can you call Observable function to download data from server every 10 seconds?

My App service

    getDevices (): Observable<Device[]> {
    return this.http.get(this.deviceUrl)
      .map(this.extractData)
      .catch(this.handleError);
}

My AppComponent

ngOnInit():void {
    this.getData();
    this.getLastDeviceInterior();
    this.getOutDevice();
    this.getSetings();
  }

  getData()
  {
    this.dataService.getDevices().subscribe( devices => this.devices = devices,
      error =>  this.errorMessage = <any>error);
  }
Lukas
  • 310
  • 3
  • 10

2 Answers2

2

You can use setInterval if using Angular

  getData()
  {
      setInterval(() => {
           this.dataService.getDevices().subscribe( devices => this.devices = devices,
           error =>  this.errorMessage = <any>error);
       }, 10);
  }

OR

   setInterval(() => {
       this.getData();
   }, 10);
mayur
  • 3,558
  • 23
  • 37
  • settimeout runs once after the set timeout. setinterval runs many times according to your intervals. http://stackoverflow.com/questions/2696692/setinterval-vs-settimeout – coderdark Dec 03 '16 at 08:49
0

There is a function in javascript to call something every x milliseconds:

http://www.w3schools.com/jsref/met_win_setinterval.asp

Xymanek
  • 1,357
  • 14
  • 25