So I currently use something like:
$(window).resize(function(){resizedw();});
But this gets called many times while resizing process goes on. Is it possible to catch an event when it ends?
So I currently use something like:
$(window).resize(function(){resizedw();});
But this gets called many times while resizing process goes on. Is it possible to catch an event when it ends?
One solution is extend jQuery with a function, e.g.:
resized
Sample usage:
$(window).resized(myHandler, 300);
This worked for what I was trying to do in chrome. This won't fire the callback until 200ms after last resize event.
since the selected answer didn't actually work .. and if you're not using jquery here is a simple throttle function with an example of how to use it with window resizing
Internet Explorer provides a resizeEnd event. Other browsers will trigger the resize event many times while you're resizing.
There are other great answers here that show how to use setTimeout and the .throttle, .debounce methods from lodash and underscore, so I will mention Ben Alman's throttle-debounce jQuery plugin which accomplishes what you're after.
Suppose you have this function that you want to trigger after a resize:
Throttle Example
In the following example,
onResize()
will only be called once every 250 milliseconds during a window resize.Debounce Example
In the following example,
onResize()
will only be called once at the end of a window resizing action. This achieves the same result that @Mark presents in his answer.This is a modification to Dolan's code above, I've added a feature which checks the window size at the start of the resize and compares it to the size at the end of the resize, if size is either bigger or smaller than the margin (eg. 1000) then it reloads.
UPDATE!
Better alternative also created by me is here: https://stackoverflow.com/a/23692008/2829600 (supports "delete functions")
ORIGINAL POST:
I wrote this simple function for handling delay in execution, useful inside jQuery .scroll() and .resize() So callback_f will run only once for specific id string.