JQuery plugin for lazy-loading/lazy-evaluation?

2020-06-06 01:11发布

Is there such jQuery plugin?

More specific: I want to use some elegant and easy way to postpone some code execution until it's really needed (some event happens). And when this event happens, the postponed code should get executed only once. Some kind of lazy initialization.

For example, apply some animation to an element not when document is ready, but when user hovers over that element.

I know how to do it the manual way but I don't like it because I have to think about checking and setting 'initialized' flag before executing anonymous function. I was wondering if it's already done (bug free, with some tasty features).

3条回答
唯我独甜
2楼-- · 2020-06-06 01:57

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).

查看更多
手持菜刀,她持情操
3楼-- · 2020-06-06 01:57

Answer to more specific question:

You don't really need any plugin do you? Just do something along these lines.

This would trigger the function postponedHeavyFunction() only after the user clicks on the element with id lazyelement and only once.

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);

Check http://jsbin.com/agora/ for a dead-stupid demonstration.


What exactly do you want. What should be lazy loaded/lazy evaluated?? Be more specific.

Lazy evaluation (as know from other languages) AFAIK is not supported in Javascript per se (as language concept). Except maybe for operators like &, |, &&, ||.

If you just want some javascript to lazy load other scripts look into this: Painless JavaScript lazy loading with LazyLoad

查看更多
狗以群分
4楼-- · 2020-06-06 02:06

Lazy Load does lazy loading of images.

查看更多
登录 后发表回答