[Vuejs]-Passing time from my function to data() time: in vuejs

0๐Ÿ‘

I have 2 thigs you can try:

1. Put this in the end of your function:

this.time = *your value*

2 Call the function after your time variable and return your value in the end of your function

data () { 
  return {    
    time: this.checkMp3SizeAndDuration();
  }
}, 

checkMp3SizeAndDuration () {  

  const files = document.getElementById('file').files
  const file = files[0] 
  const reader = new FileReader()
  const audio = document.createElement('audio')
 
  reader.onload = function (e) {
    audio.src = e.target.result 
    let time = '' 
    audio.onloadedmetadata = () => {
      const seconds  = audio.duration
      const duration = moment.duration(seconds, 'seconds') 
      const hours = duration.hours()
      if (hours > 0) { time = `${hours}:` } 
      time = `${ duration.minutes()  }:${duration.seconds()}`  
      console.log(time) <-- Example time log: 3:51
      this.time = time // Don't work here..
    } 
    audio.addEventListener('onerror', function () {
      alert('Cannot get duration of this file.')
    }, false)
  }  
  reader.readAsDataURL(file) 

  return *your value*
}

Leave a comment