How to wait for the 'end' of 'resize' event and only then perform an action?

后端 未结 24 1650
深忆病人
深忆病人 2020-11-22 09:39

So I currently use something like:

$(window).resize(function(){resizedw();});

But this gets called many times while resizing process goes o

24条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 10:04

    You can store a reference id to any setInterval or setTimeout. Like this:

    var loop = setInterval(func, 30);
    
    // some time later clear the interval
    clearInterval(loop);
    

    To do this without a "global" variable you can add a local variable to the function itself. Ex:

    $(window).resize(function() {
        clearTimeout(this.id);
        this.id = setTimeout(doneResizing, 500);
    });
    
    function doneResizing(){
      $("body").append("
    done!"); }

提交回复
热议问题