user6069595
user6069595

Reputation: 43

Howto get currentTime inside timeupdate event (apply & bind)

How to get curentTime from Html5 Audio.I did not place audio html tag . I just want to do it in typescript.i get TypeError: Cannot read property 'currentTime' of undefined. I think there is something wrong with current context ? how can i fix it with apply & bind or any other technique ?

//player.ts

 audio = new Audio(); 
btnPlay(){ 
    this.audio.src = "media/Audio_1.mp3";     
    this.audio.load();
    this.audio.play();
    this.audio.addEventListener('timeupdate',this.timeupdate)
 }
timeupdate(e){  
  console.log(this.audio.currentTime); 
}

Upvotes: 0

Views: 1411

Answers (1)

jmsn
jmsn

Reputation: 1080

Make timeupdate an arrow function:

timeupdate = (e) => {  
  console.log(this.audio.currentTime); 
}

See https://www.typescriptlang.org/docs/handbook/functions.html#this-and-arrow-functions for more information.

Upvotes: 3

Related Questions