0

I'm trying to add a delay to the second sound, so it only plays after the first sound is finished playing. How can I do that? I've tried setTimeout, but it doesn't work.

 var notify = new Audio('resources/vox/notify.mp3');
 var hello = new Audio('resources/vox/hello.mp3');

 var commands = {
            'hi': function () {
            notify.load();
            notify.play();
            hello.load();
            setTimeout(hello.play(), 3000);
            $('#hello_r').show("slow");
        }
    };
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • possible duplicate of [why is this setTimeout not working](http://stackoverflow.com/questions/5116223/why-is-this-settimeout-not-working) and many, many others – JJJ Nov 14 '14 at 16:42

2 Answers2

1

try

setTimeout(function() { hello.play(); }, 3000);
euvl
  • 4,716
  • 2
  • 25
  • 30
1

You need an anonymous function wrapper in the callback of setTimeout:

setTimeout(function() {
    hello.play();
}, 3000);

If you're rendering your audio file in an audio tag, you can also do:

<audio id="Notify" onended="playHello();"></audio>

Supposing setTimeout is a rather hack-y way to approach this. Refer to this link for more info on DOM ended property.

Xenyal
  • 2,136
  • 2
  • 24
  • 51