views:

431

answers:

3

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

+1  A: 

Lazy Load does lazy loading of images.

Nikolas Stephan
+1  A: 

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

jitter
Edited my question. It's more specific now.
HappyCoder
+1 for the Painless Javascript Lazy Loading article.
Robert Harvey
Justin Johnson
@Justin Johnson: I suppose you talk about the LazyLoad script. Did you try if it works in IE8? I guess it simply hasn't been tested for newer versions IE8, Chrome, Opera10. Maybe contact author and suggest updating supported browser version info
jitter
Hm... Yeah, there is jQuery.one (http://docs.jquery.com/Events/one#typedatafn) that does the same and runs great. Unfortunately, won't help in non-trivial cases.
HappyCoder
+2  A: 

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

HappyCoder
Interesting find. But one question comes to mind. (Sorry) How much time did you spend on finding this instead of coding ;)
jitter
About 5 minutes actually. It's not the matter of saved time. It's pure developer's curiosity and willing to communicate. That's what the stackoverflow is for, isn't it? :)
HappyCoder