知道所有其他准备好的回调何时完成 [英] Knowing when all other ready callbacks are done

查看:142
本文介绍了知道所有其他准备好的回调何时完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 ready 事件的处理程序会在所有其他处理程序完成后触发。

它非常方便用于操纵插件的不良操作。

I want my handler of the ready event will fire after all other handlers are done.
It's extremely handy for manipulating plugins' undesired actions.

如果我在所有其他人之后编写我的处理程序,它只能保证在所有其他解雇之后它会触发,而不是完成:

If I write my handler after all others, it only guarantees it will fire after all others fired, not finished:

$(function() {
    setTimeout(function() { alert('other handler'); }, 500);
});


$(function() { alert('my handler'); });​

小提琴

在该代码中,我的处理程序首先收到警报。

In that code, my handler alerted first.

我在jQuery版本之前阅读 1.4 readyList 是公开的。所以在版本 1.7 我不知道如何判断我的处理程序是否是最后一个处理程序。

I read that before jQuery version 1.4 the readyList was public. so in version 1.7 I have no idea how I can tell that my handler is the last handler or not.

推荐答案

如果想法是你不控制其他就绪处理程序,那么给出你的例子,其他处理程序使用 setTimeout ,你如果您的代码将在所有其他代码之后运行,则实际上永远不会知道(不检查其他代码)。

If the idea is that you don't control the other ready handlers, then given your example where another handler used a setTimeout, you can never actually know (without inspecting the other code) if your code will run after all other code.

readyList 即使是公开的也无济于事,因为在你的例子中,带有<的处理程序code> setTimeout 将在 setTimeout 处理程序之前很久从 readyList 中删除运行。 readyList 数组也无法控制那种异步代码。

The readyList wouldn't help even if it was public, because in your example, the handler with the setTimeout will be removed from the readyList long before the setTimeout handler runs. The readyList Array doesn't have any control over that sort of asynchronous code either.

所以如果你不控制(不能修改)其他代码,那么我真的没有解决方案。但是如果其他代码只是长时间运行,而不是异步,那么就没有任何问题,因为如果你的代码是最后一个 .ready()分配了处理程序,其他处理程序执行的时间长短无关紧要。如果他们的代码是同步,它将迫使你的代码等到它们完成。只是如果他们使用异步代码,比如 setTimeout 示例,那么除了检查其他代码之外没有什么可以做的,并且修改你的以确保它最后发生。

So if you don't control (can't modify) the other code, then I really don't have a solution. But if the other code is just long running, but not asynchronous, then there wouldn't be any issue, because if your code is the last .ready() handler assigned, it shouldn't matter how long the other handlers take to execute. If their code is synchronous, it will force yours to wait until they're complete. It's just that if they're using asynchronous code, like your setTimeout example, then there's nothing you can do short of examining the other code, and modifying yours to make sure it fires last.

这篇关于知道所有其他准备好的回调何时完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆