JQuery: How to call RESIZE event only once it's FINISHED resizing?

后端 未结 8 1290
一向
一向 2020-11-22 07:29

How do I call a function once the browser windows has FINISHED resizing?

I\'m trying to do it like so, but am having problems. I\'m using the JQuery Resize

8条回答
  •  旧巷少年郎
    2020-11-22 07:58

    This is my approach:

    document.addEventListener('DOMContentLoaded', function(){
        var tos = {};
        var idi = 0;
        var fn  = function(id)
        {
            var len = Object.keys(tos).length;
    
            if(len == 0)
                return;
    
            to = tos[id];
            delete tos[id];
    
            if(len-1 == 0)
                console.log('Resize finished trigger');
        };
    
        window.addEventListener('resize', function(){
            idi++;
            var id = 'id-'+idi;
            tos[id] = window.setTimeout(function(){fn(id)}, 500);
        });
    });
    

    The resize-event-listener catches all incoming resize calls, creates a timeout-function for each and saves the timeout-identifier along with an iterating number prepended by 'id-' (to be usable as array key) in the tos-array.

    each time, the timout triggers, it calls the fn-function, that checks, if that was the last timeout in the tos array (the fn-function deletes every executed timout). if true (= if(len-1 == 0)), the resizing is finished.

提交回复
热议问题