You cannot prevent these events from starting. They always do it. What you want to do is immediately stop listening and then process the event to avoid repeating. Then the entire handler is configured again after setTimeout. There is no more recursion if someone does not resize the window. I use 5000ms here, since itโs easier to see how it works in the console. You should not see more than one spam in the FF console every 5 seconds, even if you resize as spam.
(function staggerListen(){ window.onresize = function(){ window.onresize = false; console.log('spam'); setTimeout(staggerListen,5000); }; })()
Using logic to decide whether to do something every time the handler fires, it still technically launches the handler and the if + lookup statement. It can become difficult.
Erik reppen
source share