JavaScript/JQuery: $(window).resize how to fire AFTER the resize is completed?

前端 未结 12 685
终归单人心
终归单人心 2020-11-22 08:54

I\'m using JQuery as such:

$(window).resize(function() { ... });

However, it appears that if the person manually resizes their browser wind

相关标签:
12条回答
  • 2020-11-22 09:23

    Assuming that the mouse cursor should return to the document after window resize, we can create a callback-like behavior with onmouseover event. Don't forget that this solution may not work for touch-enabled screens as expected.

    var resizeTimer;
    var resized = false;
    $(window).resize(function() {
       clearTimeout(resizeTimer);
       resizeTimer = setTimeout(function() {
           if(!resized) {
               resized = true;
               $(document).mouseover(function() {
                   resized = false;
                   // do something here
                   $(this).unbind("mouseover");
               })
           }
        }, 500);
    });
    
    0 讨论(0)
  • 2020-11-22 09:29

    If you have Underscore.js installed, you could:

    $(window).resize(_.debounce(function(){
        alert("Resized");
    },500));
    
    0 讨论(0)
  • 2020-11-22 09:30

    Many thanks to David Walsh, here is a vanilla version of underscore debounce.

    Code:

    // Returns a function, that, as long as it continues to be invoked, will not
    // be triggered. The function will be called after it stops being called for
    // N milliseconds. If `immediate` is passed, trigger the function on the
    // leading edge, instead of the trailing.
    function debounce(func, wait, immediate) {
        var timeout;
        return function() {
            var context = this, args = arguments;
            var later = function() {
                timeout = null;
                if (!immediate) func.apply(context, args);
            };
            var callNow = immediate && !timeout;
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
            if (callNow) func.apply(context, args);
        };
    };
    

    Simple usage:

    var myEfficientFn = debounce(function() {
        // All the taxing stuff you do
    }, 250);
    
    $(window).on('resize', myEfficientFn);
    

    Ref: http://davidwalsh.name/javascript-debounce-function

    0 讨论(0)
  • 2020-11-22 09:31

    Some of the previously mentioned solutions did not work for me, even though they are of more general usage. Alternatively I've found this one that did the job on window resize:

    $(window).bind('resize', function(e){
        window.resizeEvt;
        $(window).resize(function(){
            clearTimeout(window.resizeEvt);
            window.resizeEvt = setTimeout(function(){
            //code to do after window is resized
            }, 250);
        });
    });
    
    0 讨论(0)
  • 2020-11-22 09:31

    This is what i've implemented:

    $(window).resize(function(){ setTimeout(someFunction, 500); });

    we can clear the setTimeout if we expect resize to happen less than 500ms

    Good Luck...

    0 讨论(0)
  • 2020-11-22 09:37

    It works for me. See this solution - https://alvarotrigo.com/blog/firing-resize-event-only-once-when-resizing-is-finished/

    var resizeId;
    $(window).resize(function() {
        clearTimeout(resizeId);
        resizeId = setTimeout(doneResizing, 500);
    });
    function doneResizing(){
        //whatever we want to do 
    }

    0 讨论(0)
提交回复
热议问题