-3

Can anyone help me understand why console.log(this.getPlayerDuration()) inside of setAttendancePromptTimeLimit() is returning NaN?

// video-player.js    

const videoP = document.querySelector('#in-course-player')

class videoPlyer {
  constructor(videoElem){
        this.vidPlayer = videoElem
    }
  play =  function(cb){
    arguments.length ? cb : console.log('play')
  }
    
  addListener = (elem, event, cb) => {
        elem.addEventListener(event, cb)
    }

  getPlayerCurrentTime = () => this.vidPlayer.currentTime

  getPlayerDuration = () => this.vidPlayer.duration

  showVideoDuration = function(cb){
    arguments.length ? cb : this.addListener(this.vidPlayer, 'loadedmetadata', () => 
        console.log(this.getPlayerDuration()))
    }


  setAttendancePromptTimeLimit = cb => {

    // returns NAN
    console.log(this.getPlayerDuration())
  }

  init = () => {
    //returns 16.1
    this.showVideoDuration()  
    //returns NAN 
    this.setAttendancePromptTimeLimit()    
  }
 }

 const coursePlayer = new videoPlyer(videoP)
 coursePlayer.init()
qb1234
  • 155
  • 2
  • 14
  • 4
    Because it doesn't know the duration yet. – tkausl Oct 10 '21 at 22:32
  • 1
    Please consider doing the most basic research yourself before asking next time. It's right in the description of the property: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/duration – connexo Oct 10 '21 at 22:46
  • Thanks guys...changed the event from 'loadedmetadata' to 'timeupdate' – qb1234 Oct 10 '21 at 22:57

1 Answers1

1

[...] If no media data is available, the value NaN is returned. [...]
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/duration

connexo
  • 53,704
  • 14
  • 91
  • 128