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

后端 未结 8 1156
一向
一向 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 08:19

    Here is an example using thejh's instructions

    You can store a reference id to any setInterval or setTimeout. Like this:

    var loop = setInterval(func, 30);
    
    // some time later clear the interval
    clearInterval(loop);
    
    0 讨论(0)
  • 2020-11-22 08:22

    Debounce.

    function debouncer( func , timeout ) {
       var timeoutID , timeout = timeout || 200;
       return function () {
          var scope = this , args = arguments;
          clearTimeout( timeoutID );
          timeoutID = setTimeout( function () {
              func.apply( scope , Array.prototype.slice.call( args ) );
          } , timeout );
       }
    }
    
    
    $( window ).resize( debouncer( function ( e ) {
        // do stuff 
    } ) );
    

    Note, you can use this method for anything you want to debounce (key events etc).

    Tweak the timeout parameter for optimal desired effect.

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