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

后端 未结 8 1316
一向
一向 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: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.

提交回复
热议问题