4

有这样的jQuery插件吗?

更具体地说:我想使用一些优雅而简单的方法来推迟一些代码执行,直到它真正需要(一些事件发生)。当这个事件发生时,被推迟的代码应该只执行一次。某种惰性初始化。

例如,不是在文档准备好时而是在用户将鼠标悬停在该元素上时对元素应用一些动画。

我知道如何以手动方式执行此操作,但我不喜欢它,因为我必须考虑在执行匿名函数之前检查和设置“初始化”标志。我想知道它是否已经完成(没有错误,具有一些美味的功能)。

4

3 回答 3

4

http://plugins.jquery.com/project/LazyReady

Lazy Ready A plugin designed to delay code initialization until specified DOM element(s) is interacted with (hovered/focused).

于 2009-11-20T21:56:07.277 回答
2

延迟加载会延迟加载图像。

于 2009-11-20T16:24:50.700 回答
2

回答更具体的问题:

你真的不需要任何插件吗?只需按照这些思路做一些事情。

这将postponedHeavyFunction()仅在用户单击具有 id 的元素lazyelement且仅一次后触发该功能。

function postponedHeavyFunction() {
    // unbind. This guarantees that postponedHeavyFunction will only
    // execute once for the event we bound it to #lazyelement
    $("#lazyelement").unbind('click', postponedHeavyFunction);
    ...
    //do whatever 
    ....
}

//when event is triggered the function you specify gets run
$("#lazyelement").bind('click', postponedHeavyFunction);

检查http://jsbin.com/agora/以获得愚蠢的演示。


你到底想要什么。应该延迟加载/延迟评估什么?更加详细一些。

延迟评估(从其他语言中知道) Javascript 本身(作为语言概念)不支持 AFAIK。&除了, |, &&,等运算符之外||

如果您只想让一些 javascript 延迟加载其他脚本,请查看:Painless JavaScript lazy loading with LazyLoad

于 2009-11-20T18:02:36.697 回答