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

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

    I had luck with the following recommendation: http://forum.jquery.com/topic/the-resizeend-event

    Here's the code so you don't have to dig through his post's link & source:

    var rtime;
    var timeout = false;
    var delta = 200;
    $(window).resize(function() {
        rtime = new Date();
        if (timeout === false) {
            timeout = true;
            setTimeout(resizeend, delta);
        }
    });
    
    function resizeend() {
        if (new Date() - rtime < delta) {
            setTimeout(resizeend, delta);
        } else {
            timeout = false;
            alert('Done resizing');
        }               
    }
    

    Thanks sime.vidas for the code!

    0 讨论(0)
  • 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(){
        //...
    });
    
    0 讨论(0)
  • 2020-11-22 10:02
    var flag=true;
    var timeloop;
    
    $(window).resize(function(){
        rtime=new Date();
        if(flag){
            flag=false;
            timeloop=setInterval(function(){
                if(new Date()-rtime>100)
                    myAction();
            },100);
        }
    })
    function myAction(){
        clearInterval(timeloop);
        flag=true;
        //any other code...
    }
    
    0 讨论(0)
  • This is a modification to Dolan's code above, I've added a feature which checks the window size at the start of the resize and compares it to the size at the end of the resize, if size is either bigger or smaller than the margin (eg. 1000) then it reloads.

    var rtime = new Date(1, 1, 2000, 12,00,00);
    var timeout = false;
    var delta = 200;
    var windowsize = $window.width();
    var windowsizeInitial = $window.width();
    
    $(window).on('resize',function() {
        windowsize = $window.width();
        rtime = new Date();
        if (timeout === false) {
                timeout = true;
                setTimeout(resizeend, delta);
            }
    });
    
    function resizeend() {
    if (new Date() - rtime < delta) {
        setTimeout(resizeend, delta);
        return false;
    } else {
            if (windowsizeInitial > 1000 && windowsize > 1000 ) {
                setTimeout(resizeend, delta);
                return false;
            }
            if (windowsizeInitial < 1001 && windowsize < 1001 ) {
                setTimeout(resizeend, delta);
                return false;
            } else {
                timeout = false;
                location.reload();
            }
        }
        windowsizeInitial = $window.width();
        return false;
    }
    
    0 讨论(0)
  • 2020-11-22 10:03

    Well, as far as the window manager is concerned, each resize event is its own message, with a distinct beginning and end, so technically, every time the window is resized, it is the end.

    Having said that, maybe you want to set a delay to your continuation? Here's an example.

    var t = -1;
    function doResize()
    {
        document.write('resize');
    }
    $(document).ready(function(){
        $(window).resize(function(){
            clearTimeout(t);
            t = setTimeout(doResize, 1000);
        });
    });
    
    0 讨论(0)
  • 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("<br/>done!");   
    }
    
    0 讨论(0)
提交回复
热议问题