Search code examples
javascriptdomonresize

DOM onresize event


If I have this

window.onresize = function() {
  alert('resized!!');
};

My function gets fired multiple times throughout the resize, but I want to capture the completion of the resize. This is in IE.

Any ideas? There are various ideas out there, but not has worked for me so far (example IE's supposed window.onresizeend event.)


Solution

  • In this case, I would strongly suggest debouncing. The most simple, effective, and reliable way to do this in JavaScript that I've found is Ben Alman's jQuery plugin, Throttle/Debounce (can be used with or without jQuery - I know... sounds odd).

    With debouncing, the code to do this would be as simple as:

    $(window).resize($.debounce(1000, function() {
       // Handle your resize only once total, after a one second calm.
       ...
    }));
    

    Hope that can help someone. ;)