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

后端 未结 24 1647
深忆病人
深忆病人 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:22

    One solution is extend jQuery with a function, e.g.: resized

    $.fn.resized = function (callback, timeout) {
        $(this).resize(function () {
            var $this = $(this);
            if ($this.data('resizeTimeout')) {
                clearTimeout($this.data('resizeTimeout'));
            }
            $this.data('resizeTimeout', setTimeout(callback, timeout));
        });
    };
    

    Sample usage:

    $(window).resized(myHandler, 300);

提交回复
热议问题