Run date-function multiple times but catches just first time


#1

Hello there,

I am trying to catch the current seconds in an intervall. But with my function, I am always just receiving the first value every time I run the code, so it is giving back the current amount of seconds only the first time it´s running.

That´s my code:

  date = new Date();
  sec: number;

  gettime(){
    this.sec= this.date.getSeconds();
    console.log("SECONDS: ", sec.start);
  }

Any ideas how to fix it??

Greetings


#2

No, because you haven’t shown how this function is ever called.


#3
date = new Date();
  mylat:number;
  mylng:number;
  start:number;
  end:number;
  timedif:number;

  ionViewDidEnter(){
    let timer = setInterval(() => {
      this.getStart().then(() => {
        this.getPosition().then(() => {
          this.getEnd().then(() => {
            this.timedif = this.end - this.start;
            console.log("TIMEDIF: ", this.timedif)
          });
        });
      })
    }, this.timedif);      
  }

  async getStart(){
    this.start = this.date.getSeconds();
    console.log("Start: ", this.start)
  }

  async getEnd(){
    this.end = this.date.getSeconds();
    console.log("End: ", this.end)
  }

  async getPosition(){
    this.geo.getCurrentPosition().then( pos => {
      this.mylat = pos.coords.latitude;
      this.mylng = pos.coords.longitude;
      console.log("LATITUDE: " + this.mylat);
      console.log("LONGITUDE: " + this.mylng);      
    }).catch(err => console.log(err));
  }

so basically it should take the time when the getCurrentPosition function starts and then the time when it´s over to use it as time how fast the intervall is repeated, so it just should repeat the intervall after finishing the previous function…