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

前端 未结 12 674
终归单人心
终归单人心 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:47

    I use the following function for delaying repeated actions, it will work for your case:

    var delay = (function(){
      var timer = 0;
      return function(callback, ms){
        clearTimeout (timer);
        timer = setTimeout(callback, ms);
      };
    })();
    

    Usage:

    $(window).resize(function() {
        delay(function(){
          alert('Resize...');
          //...
        }, 500);
    });
    

    The callback function passed to it, will execute only when the last call to delay has been made after the specified amount of time, otherwise a timer will be reset, I find this useful for other purposes like detecting when the user stopped typing, etc...

提交回复
热议问题