score:1

Accepted answer

your problem is due to the value of audioduration is set only in callback and the console.log is used directly after onloadedmetadata so the console.log will run before the value is set. two ways to fix that, one way is to do console.log inside onloadmetadata. the other way is to return a promise and await for the result.

const audioduration = {}
const getduration = () => new promise(resolve => {
  convertedaudio.onloadedmetadata = () => {
    resolve(convertedaudio.duration);
  }

})

getduration().then(l => { console.log(l); audioduration.length = l; })

score:0

try this

var audioduration = {};


convertedaudio.onloadedmetadata = () => {

    if(convertedaudio.duration!=undefined){audioduration.length = convertedaudio.duration}

};

console.log (audioduration) // displays object {length: 25.547755}
console.log (audioduration.length) // displays idk, u see what it does since i can't replicated convertedaudio

Related Query

More Query from same tag