0

Possible Duplicate:
javascript resize event firing multiple times while dragging the resize handle

Tried :

$(window).bind('load resize', function (e) {
    console.log("resized");
});

but it wrote "resized" for every small movement of the window. Can I call a function (In this example, that console.log()) only when I finish (I stop) to resize the window?

Thank you!

Community
  • 1
  • 1
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • The resize event fires continuously while the dimensions of the window are changing; there's no event that would signal that resizing has finished. – Cᴏʀʏ Aug 21 '12 at 13:34
  • 2
    This post in particular has your answer http://stackoverflow.com/a/668185/54746 – CaffGeek Aug 21 '12 at 13:35

2 Answers2

2

Maybe you simply define resize is finished if there is no new resize event within the next 100 ms. Like this:

var resizeEnd;
$(window).bind('resize', function(e) {
    clearTimeout(resizeEnd);
    resizeEnd = setTimeout(function() {
        console.log("resized");
    }, 100);
});
kannix
  • 5,107
  • 6
  • 28
  • 47
  • Why your works and mine not? http://jsfiddle.net/FpXQq/ – markzzz Aug 21 '12 at 13:48
  • 1
    .setTimeout needs a function as argument. you need to change it to timerResizer = setTimeout(function () {console.log("resized")}, 1000); – kannix Aug 21 '12 at 13:53
  • 1
    Besides you don't need that if (timerResizer !== false) check.. calling clearTimeout with undefined as argument is no problem ;) – kannix Aug 21 '12 at 13:55
0

Resize would continuously fire while the window is being resized and it wouldn't fire when it is not being resized. So there isn't a way for you to recognize when a continuous resizing has stopped, so stick to logic that works upon firing of the resize event.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100