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

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

    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;
    }
    

提交回复
热议问题