如何在延迟后加载 window.onload 中的 javascript 函数

发布于 2024-10-31 07:46:55 字数 328 浏览 2 评论 0原文

我有几个函数应该在 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 技术交流群。

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

发布评论

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

评论(4

断念 2024-11-07 07:46:55

我很惊讶这两个函数都能在任何浏览器中调用。但是您可能会拥有更好的运气,例如:

function foo() {
    // javascript code
    setTimeout(bar, additionalDelay);
}

function bar() {
    // javascript code
}

window.onload = function() { setTimeout(foo, delay); };

编辑:没关系,我明白为什么两个超时都会执行。当您执行以下操作时:

window.onload = setTimeout(bar, delay);

...您实际上并没有设置 window.onload 在延迟后执行您的函数。相反,这是立即调用 setTimeout() 来安排函数调用,并将结果(安排的函数调用的句柄)分配给 window.onload。这是不正确的,并且当某些浏览器尝试将 window.onload 作为函数调用时,可能会导致运行时错误。

您想要做的是将一个函数分配给 window.onload,例如:

window.onload = function() { 
    setTimeout(foo, delay); 
    setTimeout(bar, delay);
};

I'm surprised both functions get invoked in any browser. But you might have better luck with something like:

function foo() {
    // javascript code
    setTimeout(bar, additionalDelay);
}

function bar() {
    // javascript code
}

window.onload = function() { setTimeout(foo, delay); };

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 invoking setTimeout() to schedule your function call, and assigning the result (a handle to the scheduled function invocation) to window.onload. This is not correct, and will probably cause a runtime error in some browsers when they try to invoke window.onload as a function.

What you want to do instead is assign a function to window.onload, like:

window.onload = function() { 
    setTimeout(foo, delay); 
    setTimeout(bar, delay);
};
摇划花蜜的午后 2024-11-07 07:46:55

您可以将 jQuery 与这样的东西一起使用:

$(document).ready(function() { /* your code */ });

或者

$(window).load(function() { /* your code */ });

jQuery 自动将函数添加到堆栈中,并在事件触发后运行它们。

如果你只想用 JS 来做,你可以创建一组函数来触发:

function a() {
        alert('a');
    }

    function b() {
        alert('b');
    }

    var arr = new Array(a, b);

    window.onload = function() {
        for(var i = 0; i < arr.length; i++) {
            setTimeout(arr[i], 1000);
        }
    }

希望它有帮助。

You could use jQuery with sth like that:

$(document).ready(function() { /* your code */ });

or

$(window).load(function() { /* your code */ });

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:

function a() {
        alert('a');
    }

    function b() {
        alert('b');
    }

    var arr = new Array(a, b);

    window.onload = function() {
        for(var i = 0; i < arr.length; i++) {
            setTimeout(arr[i], 1000);
        }
    }

Hope it helps.

尝蛊 2024-11-07 07:46:55

尝试将超时包装在函数中:

window.onload = function(delay) {
                  setTimeout(foo, delay); 
                  setTimeout(bar, delay); 
                };

Try wrapping the timeout in a function:

window.onload = function(delay) {
                  setTimeout(foo, delay); 
                  setTimeout(bar, delay); 
                };
你与昨日 2024-11-07 07:46:55

我可以在您的代码中看到两个基本错误。首先,如果你想传递一个函数作为参数,你需要写出不带括号的函数名;如果添加括号,该函数将立即执行。看一个例子:

function foo(){
    alert("I am foo");
}
function bar(){
    alert("I am bar");
}

setTimeout(foo, 5000);
setTimeout(bar(), 10000);

其次,如果您使用 = 运算符为 .onload 属性赋值,您将覆盖其先前的值,就像 a = 3 一样 覆盖之前的 a 值。

function foo(){
    alert("I am foo");
}
function bar(){
    alert("I am bar");
}
window.onload = foo;
window.onload = bar;

据我所知,您的主要问题是能够添加而不是替换事件处理程序。与往常一样,不存在可以在所有浏览器中安全使用的通用 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:

function foo(){
    alert("I am foo");
}
function bar(){
    alert("I am bar");
}

setTimeout(foo, 5000);
setTimeout(bar(), 10000);

Secondly, if you assign a value to the .onload attribute with the = operator you'll overwrite its previous value, just like a = 3 overwrites previous the value of a.

function foo(){
    alert("I am foo");
}
function bar(){
    alert("I am bar");
}
window.onload = foo;
window.onload = bar;

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.

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