devinsays / portfolio-press

A WordPress theme for artists and designers to showcase their work.
https://wptheming.com/portfolio-theme/
GNU General Public License v2.0
62 stars 30 forks source link

Throttle or Debounce window resize event #59

Closed mfields closed 10 years ago

mfields commented 10 years ago

theme.js line 64:

$(window).on('resize', function(){
    portfolio_desktop_submenus();
    portfolio_mobile_submenus();
});

This has the potential to fire for every pixel the browser "visits" during a resize. While some browsers handle it better than others, it's still a lot of work. It's best to throttle or debounce such methods.

Here is a good solution: http://benalman.com/projects/jquery-throttle-debounce-plugin/

devinsays commented 10 years ago

Thanks for the tip. Also related: http://www.paulirish.com/2009/throttled-smartresize-jquery-event-handler/

devinsays commented 10 years ago

Ended up using Remy Sharp's method.

http://remysharp.com/2010/07/21/throttling-function-calls/

    function debounce(fn, delay) {
        var timer = null;
            return function () {
            var context = this, args = arguments;
            clearTimeout(timer);
            timer = setTimeout(function () {
              fn.apply(context, args);
            }, delay);
        };
    }