Solution 1 :

You’re not passing the timeout a function. It needs to say:

  () => alert("Totaler Absturz. Score: " + Math.floor(counter / 100)),

It’s the () => ... bit that will wrap your alert in a function. If it’s not wrapped in a function, it will execute immediately.


Problem :

I need a sound to play while or before a score alert pops up (simple dinosaur game). Right now it just shows the alert and then it plays the sound. I have tried to set a timeout but it doesn’t work.

if (blockLeft < 200 && blockLeft > 0 && characterTop >= 300) {
    const death = new Audio((URL = "/sound/death.mp3"));;
    death.volume = 0.4; = "none"; = "none";
      alert("Totaler Absturz. Score: " + Math.floor(counter / 100)),
    counter = 0;


Comment posted by Javascript do something before alert

Does this answer your question?

Comment posted by ffriedrich

I put in the function that plays the sound (exactly how you suggested) and it still sends the alert before the sound plays. Gruß

Comment posted by Dennis Hackethal

How large of a file is the mp3/how long does it take to load it?

Comment posted by ffriedrich

the audio file has a size of 50KB and it plays immediatly after I click ok on the alert

Comment posted by ffriedrich

hey man I managed to get it to work. turned out github took a bit to load everything.. Im still trying to figure out how everything works yk. have nice day