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

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

    Actually, as I know, you can't do some actions exactly when resize is off, simply because you don't know future user's actions. But you can assume the time passed between two resize events, so if you wait a little more than this time and no resize is made, you can call your function.
    Idea is that we use setTimeout and it's id in order to save or delete it. For example we know that time between two resize events is 500ms, therefore we will wait 750ms.

    var a;
    $(window).resize(function(){
      clearTimeout(a);
      a = setTimeout(function(){
        // call your function
      },750);
    });

提交回复
热议问题