Search code examples
javascriptinternet-explorer-7timeoutonresize

Why is the resize event constantly firing when I assign it a handler with a timeout?


I assigned a timeout to my window.resize handler so that I wouldn't call my sizable amount resize code every time the resize event fires. My code looks like this:

<script>
function init() {
  var hasTimedOut = false;
  var resizeHandler = function() {
    // do stuff
    return false;
  };

  window.onresize = function() {
    if (hasTimedOut !== false) {
      clearTimeout(hasTimedOut);
    }
    hasTimedOut = setTimeout(resizeHandler, 100); // 100 milliseconds
  };
}
</script>
<body onload="init();">
...
etc...

In IE7 (and possibly other versions) it appears that when you do this the resize event will constantly fire. More accurately, it will fire after every timeout duration -- 100 milliseconds in this case.

Any ideas why or how to stop this behavior? I'd really rather not call my resize code for every resize event that fires in a single resizing, but this is worse.


Solution

  • In your //do stuff code, do you manipulate any of the top,left,width,height,border,margin or padding properties?

    You may unintentionally be triggering recursion which unintentionally triggers recursion which unintentionally triggers recursion...