JavaScript Mac Firefox setInterval() 怪异
我第一次遇到 safari 的问题,当函数名称没有用引号括起来时(并且可选地它与添加的括号接缝),设置时间间隔的行为会不可预测:
repeatInterval = setInterval("foo()", 50);
在将我的代码更改为以这种方式读取时,它接缝它不会被执行在 Mac 版本的 Firefox 中完全没有。
我做了一些进一步的测试,确保它在 Linux 和 Windows 版本(包括 Windows 下的 3.0.10 和 3.6)下完全正常工作。唯一出现此问题的组合是 Mac OS X Snow Leopard 上的 Firefox(本例中为 3.6)。
除非以以下格式编写,否则它根本不起作用:
repeatInterval = setInterval(foo, 50);
是否有解决此问题的解决方案,该解决方案可以在 Mac 上的所有其他浏览器和 Firefox 中工作,而无需在 javascript 中测试操作系统和浏览器并对其进行相应的破解?
I first encountered a problem with safari, where set interval would behave unpredicatbly when the function name was not enclosed within quotations (and optionally it seams with added parentheses):
repeatInterval = setInterval("foo()", 50);
Upon changing my code to read in this way, it seams it does not get executed at all in the Mac version of Firefox.
I did some further testing an ensured that it works completely fine under linux and windows versions (including both 3.0.10 and 3.6 under windows). The only combination that throws up this problem is Firefox (3.6 in this case) on Mac OS X Snow Leopard.
It dose not work at all unless written in the following format:
repeatInterval = setInterval(foo, 50);
Is there a sollution to this problem that will work in all other browsers and Firefox on the Mac, without testing for the operating system and browser in the javascript and hacking it to work accordingly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将字符串作为
setInterval
或setTimeout
的第一个参数传递。您应该传递函数标识符(如使用setInterval(foo, 50);
所做的那样)或传递匿名函数(使用function
关键字)。Don't pass a string as the first parameter of
setInterval
orsetTimeout
. You should either pass a function identifier (as you did withsetInterval(foo, 50);
) or pass an anonymous function (using thefunction
keyword).