1

This is my code:-

    function playAudio(sound,audioName)
    {
        sound = new Howl({
            src: ['/audio/'+audioName]
        });
        sound.play();
        return sound;
    }

    var sound;
    setTimeout(playAudio,5000,sound,'smooth_criminal.mp3');

I am passing function playAudio to function setTimeout. playAudio() function returns a value which I want to catch. Is there a way I can get the return value of playAudio.

I am looking for a solution something similar to,

setTimeout(sound = playAudio,5000,sound,'smooth_criminal.mp3');
Imran Ali
  • 2,223
  • 2
  • 28
  • 41
Parag Kadam
  • 3,620
  • 5
  • 25
  • 51

1 Answers1

1

You can do this:

setTimeout(function () {
    var sound = playAudio(sound,'smooth_criminal.mp3');
    // do something with sound....
}, 5000);

This creates an anonymous function on-the-fly as you pass it to setTimeout. In it you can write all the code you want...

Just make sure to use the value of sound only when you have called playAudio, so within that anonymous function.

For example, this will not work as one might expect:

var sound;
setTimeout(function () {
    sound = playAudio(sound,'smooth_criminal.mp3');
}, 5000);
// not the right place to do something with sound....
// as it is not available yet.
console.log(sound);
trincot
  • 317,000
  • 35
  • 244
  • 286