javascript assigning variable inside a callback function return undefined -
this question has answer here:
i have simple script play audio like:
var audio = new audio(music) var music_duration audio.addeventlistener('loadedmetadata', function() { var duration = audio.duration console.log(duration) music_duration = duration }) console.log("checking duration") console.log(music_duration)
here music_duration
returns undefined..
while duration
inside addeventlistener
gives duration of music.
i new javascript ... wrong in here ??
console.log("checking duration") console.log(music_duration)
will executed before loadedmetadata event emitted.
you can check editing script in way:
var audio = new audio(music) var music_duration audio.addeventlistener('loadedmetadata', function() { console.log('here') var duration = audio.duration console.log(duration) music_duration = duration }) console.log('there') console.log("checking duration") console.log(music_duration)
you'll see there
printed before here
.
Comments
Post a Comment