How do you make your code wait, before the next step is interpreted?

javascript

#1

Hi guys, So I have been messing about with the audio element in html, and I have recently encountered a problem:

this is the code I want to use:

<audio id="myAudio"> 
<scource src="--whatever you want--" type="audio/mp3">
</audio>

<script>
playAudio()
{
     myAudio.play();

     if(myAudio.currentTime == myAudio.duration)
     {
          nextFunction();
     }
}

nextFunction()
{
     alert("here");
}
</script>

But unfortunatly the if statement condition is never ment, since the interpreter thingy does not wait until the audio track is finished playing before it goes and interprets the next line. Instead it goes right away and the currentTime is always = 0, which is not the duration time, thus the condition is false.

So my question being, is there anyway I can make the interpreter thingy wait until the audio is finished before it checks the next line?


#2

Hi,

I am not sure if it works, but you can try this.


#3

hey, I am already calling my newFunction() after a setTimeout delay (i dont't put that into my example since I didn't seem necessary) I could call another function like this

setTimeout(anotherFunc(), myAudio.duration)
function anotherFunc ()
{
setTimeout(nextFunction(), "any value")
}

but this way is bulky and I want this to be last resort, I want to make my program as compact as possible, so if there is anyother way to do this, can someone please tell me?

or can I put a setTimeout() inside of a setTimeout?

setTimeout(setTimeout(nextfunction(), "any value"), myAudio.duration)