Home > OS >  How to dynamically calculate the duration of the 'Call' class object
How to dynamically calculate the duration of the 'Call' class object

Time:07-24

Working on an Angular I'm trying to display a list of calls with their respective duration:

export class Call {
  ucid: string;
  state: string;
  originator: Originator;
  destination: Destination;
  language: string;
  server: string;
  creationDate: number;
  duration: string;
}

I'm able to get an Call[] from a Web API:

 ngOnInit(): void {
    this.callService.getCalls().subscribe(calls => {
      this.calls = calls;
     });
  }

Having the creationDate as starting point I want to calculate and display the duration of each call in a format hh:mm:ss.

Have tried this with no result:

export class Call {

  ucid: string;
  state: string;
  originator: Originator;
  destination: Destination;
  language: string;
  server: string;
  creationDate: number;
  calculateDuration = () => {
    setInterval(function () {
      let duration = this.creationDate   1;
      this.duration = duration;
      return duration;
    }, 1000);
  };
  duration: any = this.calculateDuration();
}

Any help?

CodePudding user response:

Oh, my answer to this question is relevant!

The short version is that TS/Angular/whatever isn't deserialising your request into actual concrete Call objects. Just base objects with the shape of it (basically just a dictionary).

You'd need to create Call objects from your response if you want to be able to use the method you have in there.

const calls = [];
response.forEach((x: Call /*not really a Call object*/) => {
    const call = new Call();
    call.x = x.x;
    call.y = x.y;
    calls.push(call);
});

this.calls = calls;

Once done, this.calls contains actual Call objects that have a calculateDuration function available for use.

  • Related