I'm using JQuery as such:
$(window).resize(function() { ... });
However, it appears that if the person manually resizes their browser windows by dragging the window edge to make it larger/smaller, the .resize
event above fires multiple times.
Question: How to I call a function AFTER the browser window resize completed (so that the event only fires once)?
Assuming that the mouse cursor should return to the document after window resize, we can create a callback-like behavior with onmouseover event. Don't forget that this solution may not work for touch-enabled screens as expected.
Some of the previously mentioned solutions did not work for me, even though they are of more general usage. Alternatively I've found this one that did the job on window resize:
Simple jQuery plugin for delayed window resize event.
SYNTAX:
Add new function to resize event
Remove the function(by declaring its ID) added earlier
Remove all functions
USAGE:
USAGE OUTPUT:
PLUGIN:
Declare globally delayed listener:
And below use listeners to
resized
event as you want:Here's a modification of CMS's solution that can be called in multiple places in your code:
Usage:
CMS's solution is fine if you only call it once, but if you call it multiple times, e.g. if different parts of your code set up separate callbacks to window resizing, then it will fail b/c they share the
timer
variable.With this modification, you supply a unique id for each callback, and those unique IDs are used to keep all the timeout events separate.
I use the following function for delaying repeated actions, it will work for your case:
Usage:
The callback function passed to it, will execute only when the last call to delay has been made after the specified amount of time, otherwise a timer will be reset, I find this useful for other purposes like detecting when the user stopped typing, etc...