Question

How do I make the javascript resize event handler happen only after the user stops resizing the window? I don't want it to get called over and over again as the user is resizing the window.

$(window).resize(function () {
            if( document.getElementById('staticImage').style.display == 'table-cell') {
                resizeWithImageMap();
            } else {
                resizeWithoutImageMap();
            }
        });

Thanks.

Était-ce utile?

La solution

Adeneo posted a nice functional example, but I'm partial to building it all into my own handler:

var resizeComplete = (function() {
  var funcs = [];
  var timer ;
  var delay = 250;

  window.onresize = function() {
    window.clearTimeout(timer);

    timer = window.setTimeout(function() {
      for (var i = 0; i < funcs.length; i++) {
        funcs[i](); // Call the func
      }
    }, delay);

  };

  function register(func) {
    funcs.push(func);
  }

  return {
    register: register
  }
}());

resizeComplete.register(function() {
  console.log("Resize complete");
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top