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)?
I prefer to create an event:
Here is how you create it:
You could have this in a global javascript file somewhere.
Many thanks to David Walsh, here is a vanilla version of underscore debounce.
Code:
Simple usage:
Ref: http://davidwalsh.name/javascript-debounce-function
It works for me. See this solution - https://alvarotrigo.com/blog/firing-resize-event-only-once-when-resizing-is-finished/
Actually, as I know, you can't do some actions exactly when resize is off, simply because you don't know future user's actions. But you can assume the time passed between two resize events, so if you wait a little more than this time and no resize is made, you can call your function.
Idea is that we use
setTimeout
and it's id in order to save or delete it. For example we know that time between two resize events is 500ms, therefore we will wait 750ms.This is what i've implemented:
$(window).resize(function(){ setTimeout(someFunction, 500); });
we can set
[clear the setTimeout][1]
if we expect resize to happen less than500ms
Good Luck...
If you have Underscore.js installed, you could: