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

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

    (function(){
        var special = jQuery.event.special,
            uid1 = 'D' + (+new Date()),
            uid2 = 'D' + (+new Date() + 1);
    
        special.resizestart = {
            setup: function() {
                var timer,
                    handler =  function(evt) {
                        var _self = this,
                            _args = arguments;
                        if (timer) {
                            clearTimeout(timer);
                        } else {
                            evt.type = 'resizestart';
                            jQuery.event.handle.apply(_self, _args);
                        }
    
                        timer = setTimeout( function(){
                            timer = null;
                        }, special.resizestop.latency);
                    };
                jQuery(this).bind('resize', handler).data(uid1, handler);
            },
            teardown: function(){
                jQuery(this).unbind( 'resize', jQuery(this).data(uid1) );
            }
        };
    
        special.resizestop = {
            latency: 200,
            setup: function() {
                var timer,
                    handler = function(evt) {
                        var _self = this,
                            _args = arguments;
                        if (timer) {
                            clearTimeout(timer);
                        }
                        timer = setTimeout( function(){
                            timer = null;
                            evt.type = 'resizestop';
                            jQuery.event.handle.apply(_self, _args);
                        }, special.resizestop.latency);
                    };
    
                jQuery(this).bind('resize', handler).data(uid2, handler);
            },
            teardown: function() {
                jQuery(this).unbind( 'resize', jQuery(this).data(uid2) );
            }
        };
    })();
    
    $(window).bind('resizestop',function(){
        //...
    });
    

提交回复
热议问题