Vael Victus
Vael Victus

Reputation: 4122

Playing Delayed Sounds In Browser With Javascript

We have a few sales teams. For these sales teams, we have a leaderboard. Each time one of them makes a sale, a sound is played over the speakerphones through a browser which refreshes the page every 2 minutes and checks for differences. Once it sees a difference, it plays the team's sound.

Previously the sounds would slur together because I'd call the function like such:

setTimeout(sounds[team_id].play(), delayTime);

I figured out that I had to make it a function reference point, by removing (). In that case it seemed I could get no way to send an argument to that reference point.

Tumbling down the rabbit hole later, after browser bugs and accidently mutings, I'm tired of the way I'm doing things. I don't think this is optimal.

I need a good way to play sound files from any location (no, I'm not hotlinking) that don't have to adhere to SetTimeout. I'm thinking something to do with jQuery's delay() function.

Upvotes: 0

Views: 287

Answers (1)

Chris Baker
Chris Baker

Reputation: 50602

jQuery's delay function is based on setTimeout, as would be any other delaying or time-based functionality provided by any other library.

I am not seeing why you are having troubles - a closure should be able to handle the situation you describe:

setTimeout(function () {
  sounds[team_id].play()
}, delayTime);

Assuming that team_id is the id of the team who's sound you'd like to play, and that team_id is available in the scope from which you call setTimeout. Without seeing the full code in context, it is difficult to pinpoint any potential problems.

Upvotes: 1

Related Questions