使用 Javascript 在浏览器中播放延迟声音

发布于 2024-11-26 20:04:58 字数 409 浏览 0 评论 0原文

我们有几个销售团队。对于这些销售团队,我们有一个排行榜。每次他们进行销售时,都会通过浏览器通过扬声器播放声音,该浏览器每 2 分钟刷新一次页面并检查差异。一旦发现差异,它就会播放团队的声音。

以前,声音会混在一起,因为我会像这样调用函数:

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

我发现我必须通过删除 () 来使其成为函数参考点。在这种情况下,我似乎无法向该参考点发送参数。

后来,在浏览器出现错误和意外静音之后,我跌入了兔子洞,我厌倦了自己做事的方式。我认为这不是最佳的。

我需要一种好方法来从任何位置播放声音文件(不,我不是热链接),而不必遵守 SetTimeout。我正在考虑与 jQuery 的delay() 函数有关的事情。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-12-03 20:04:59

jQuery 的延迟函数基于 setTimeout,任何其他库提供的任何其他延迟或基于时间的功能也是如此。

我不明白你为什么遇到麻烦 - 闭包应该能够处理你描述的情况:

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

假设 team_id 是你想参加的球队的 ID,并且 < code>team_id 在您调用 setTimeout 的范围内可用。如果没有在上下文中查看完整的代码,就很难查明任何潜在的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文