如何在延迟后加载 window.onload 中的 javascript 函数
我有几个函数应该在 onload 事件延迟后触发。它在 Chrome 中运行良好,但在 Firefox 中则不行。
function foo() {
// javascript code
}
window.onload = setTimeout(foo, delay);
function bar() {
// javascript code
}
window.onload = setTimeout(bar, delay);
如果我删除延迟,则在 Firefox 中调用“bar”,在 Chrome 中调用“foo”和“bar”。这里可能有什么问题?
I have couple of functions that should be triggered after some delay in onload event. It works fine in chrome but not in Firefox.
function foo() {
// javascript code
}
window.onload = setTimeout(foo, delay);
function bar() {
// javascript code
}
window.onload = setTimeout(bar, delay);
If I remove the delay, 'bar' gets invoked in Firefox and 'foo' and 'bar' get invoked in chrome. What could be the issue here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我很惊讶这两个函数都能在任何浏览器中调用。但是您可能会拥有更好的运气,例如:
编辑:没关系,我明白为什么两个超时都会执行。当您执行以下操作时:
window.onload = setTimeout(bar, delay);
...您实际上并没有设置
window.onload
在延迟后执行您的函数。相反,这是立即调用setTimeout()
来安排函数调用,并将结果(安排的函数调用的句柄)分配给window.onload
。这是不正确的,并且当某些浏览器尝试将window.onload
作为函数调用时,可能会导致运行时错误。您想要做的是将一个函数分配给
window.onload
,例如:I'm surprised both functions get invoked in any browser. But you might have better luck with something like:
Edit: Nevermind, I see why both of the timeouts execute. When you do something like:
window.onload = setTimeout(bar, delay);
...you are not actually setting
window.onload
to execute your function after a delay. Instead this is immediately invokingsetTimeout()
to schedule your function call, and assigning the result (a handle to the scheduled function invocation) towindow.onload
. This is not correct, and will probably cause a runtime error in some browsers when they try to invokewindow.onload
as a function.What you want to do instead is assign a function to
window.onload
, like:您可以将 jQuery 与这样的东西一起使用:
或者
jQuery 自动将函数添加到堆栈中,并在事件触发后运行它们。
如果你只想用 JS 来做,你可以创建一组函数来触发:
希望它有帮助。
You could use jQuery with sth like that:
or
jQuery automatically adds functions to stack, and run them all once event is triggered.
If you want do it with only JS, you can make array of functions to trigger:
Hope it helps.
尝试将超时包装在函数中:
Try wrapping the timeout in a function:
我可以在您的代码中看到两个基本错误。首先,如果你想传递一个函数作为参数,你需要写出不带括号的函数名;如果添加括号,该函数将立即执行。看一个例子:
其次,如果您使用
=
运算符为.onload
属性赋值,您将覆盖其先前的值,就像a = 3 一样
覆盖之前的a
值。据我所知,您的主要问题是能够添加而不是替换事件处理程序。与往常一样,不存在可以在所有浏览器中安全使用的通用 API。如果您使用框架或库,它可能提供跨浏览器机制。否则,您需要自己查找或编写。过去,我在几个项目中使用过它:
...虽然它自 2005 年以来就没有更新过,所以我会确保它在最新的浏览器中正常工作。
I can see two base errors in your code. First of all, if you want to pass a function as argument you need to write the function name without parenthesis; if you add the parenthesis the function will be executed right then. See an example:
Secondly, if you assign a value to the
.onload
attribute with the=
operator you'll overwrite its previous value, just likea = 3
overwrites previous the value ofa
.From what I grasp, your main problem is to be able to add rather than replace event handlers. As usual, there isn't a universal API you can safely use in all browsers. If you are using a framework or library, it probably provides a cross-browser mechanism. Otherwise, you need to find or write your own. In the past, I've used this in several projects:
... although it's not been updated since 2005 so I'd make sure it works properly in recent browsers.