JQuery plugin for lazy-loading/lazy-evaluation?

前端 未结 3 1544
北海茫月
北海茫月 2021-02-10 16:42

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 w

3条回答
  •  心在旅途
    2021-02-10 17:26

    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

提交回复
热议问题