javascript assigning variable inside a callback function return undefined -


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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -