0

I am using the popular jquery plugin Masonry to fit my columns nicely in my layout. I am using a setInterval to solve the exact same problem as BoltHead had here: JQuery, setTimeout not working

The solution is to use setTimeout to update the masonry plugin every second like this:

$(function() {
  setInterval(update, 500);
});

    function update() {
        var $container = $('#packages');
        $container.imagesLoaded(function(){
          $container.masonry({
            itemSelector : '.mainPackage',
            columnWidth : 316,
            singleMode: true,
            gutterWidth: 15
          });
        });
    }

Is this a bad idea as far as browser performance? I would think that jquery running this rather hefty function every second would slow things down. Is this a bad practice? The reason I am doing this is because I am using .slideToggle to slidedown more content, thereby needed masonry to readjust the layout. Any thoughts on this solution?

Community
  • 1
  • 1
JCHASE11
  • 3,901
  • 19
  • 69
  • 132

3 Answers3

3

slideToggle receives callback as second argument, so update your plugin there, something like this:

slideToggle(time, function(){
    update(); // this is your "update" function from question example
});

and yes, it is a bad idea to do that update periodically for no reason.

Aleksandar Vucetic
  • 14,715
  • 9
  • 53
  • 56
0

This depends only on how many block the plugin needs to "masonrify". If you have something like 20-50 blocks, it will be no problem to run the update every 500ms. This is from my personal experience with a case exactly like yours.

Sych
  • 1,849
  • 16
  • 19
0

In my experience, running something like this will slow the browser down. You may not notice it at first, but the longer the script is running the worse it will get. I really like @vucetica answer.

Eric Witchin
  • 574
  • 2
  • 8