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

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

    since the selected answer didn't actually work .. and if you're not using jquery here is a simple throttle function with an example of how to use it with window resizing

        function throttle(end,delta) {
    
        var base = this;
    
        base.wait = false;
        base.delta = 200;
        base.end = end;
    
        base.trigger = function(context) {
    
            //only allow if we aren't waiting for another event
            if ( !base.wait ) {
    
                //signal we already have a resize event
                base.wait = true;
    
                //if we are trying to resize and we 
                setTimeout(function() {
    
                    //call the end function
                    if(base.end) base.end.call(context);
    
                    //reset the resize trigger
                    base.wait = false;
                }, base.delta);
            }
        }
    };
    
    var windowResize = new throttle(function() {console.log('throttle resize');},200);
    
    window.onresize = function(event) {
        windowResize.trigger();
    }
    

提交回复
热议问题