My actual project is more complex, but I’ve simplified my problem to the code below. How come when I press the button, JavaScript will only play the first sound? I’ve noticed that when I reduce the wait time to 500ms, both sounds play. But larger numbers like 1000ms don’t work. What’s happening and why isn’t the second sound playing?
const delay = millis => new Promise((resolve, reject) => {
setTimeout(_ => resolve(), millis)
});
var firstAudio = new Audio('https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3');
var secondAudio = new Audio('https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3');
async function testAudio() {
firstAudio.play();
await delay(1000);
secondAudio.play();
}
<button onclick="testAudio()">
<h1>Test the audio</h1>
</button>