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

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

    i wrote a litte wrapper function on my own...

    onResize  =   function(fn) {
        if(!fn || typeof fn != 'function')
            return 0;
    
        var args    = Array.prototype.slice.call(arguments, 1);
    
        onResize.fnArr    = onResize.fnArr || [];
        onResize.fnArr.push([fn, args]);
    
        onResize.loop   = function() {
            $.each(onResize.fnArr, function(index, fnWithArgs) {
                fnWithArgs[0].apply(undefined, fnWithArgs[1]);
            });
        };
    
        $(window).on('resize', function(e) {
            window.clearTimeout(onResize.timeout);
            onResize.timeout    = window.setTimeout("onResize.loop();", 300);
        });
    };
    

    Here is the usage:

    var testFn  = function(arg1, arg2) {
        console.log('[testFn] arg1: '+arg1);
        console.log('[testFn] arg2: '+arg2);
    };
    
    // document ready
    $(function() {
        onResize(testFn, 'argument1', 'argument2');
    });
    

提交回复
热议问题