使用 Javascript 在浏览器中播放延迟声音
我们有几个销售团队。对于这些销售团队,我们有一个排行榜。每次他们进行销售时,都会通过浏览器通过扬声器播放声音,该浏览器每 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery 的延迟函数基于
setTimeout
,任何其他库提供的任何其他延迟或基于时间的功能也是如此。我不明白你为什么遇到麻烦 - 闭包应该能够处理你描述的情况:
假设
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:
Assuming that
team_id
is the id of the team who's sound you'd like to play, and thatteam_id
is available in the scope from which you callsetTimeout
. Without seeing the full code in context, it is difficult to pinpoint any potential problems.