Skip to content

Instantly share code, notes, and snippets.

@pablocattaneo
Last active September 16, 2017 00:14
Show Gist options
  • Save pablocattaneo/6afa37189b46f4ceeee4 to your computer and use it in GitHub Desktop.
Save pablocattaneo/6afa37189b46f4ceeee4 to your computer and use it in GitHub Desktop.
Se dispara el evento sólo una vez luego de hacer el resize al browser Just one fire after rize windows browser.
http://stackoverflow.com/questions/2854407/javascript-jquery-window-resize-how-to-fire-after-the-resize-is-completed
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.
/* Function to avoid javascript execute the funttion twice in the resize event */
var waitForFinalEvent = (function () {
var timers = {};
return function (callback, ms, uniqueId) {
if (!uniqueId) {
uniqueId = "Don't call this twice without a uniqueId";
}
if (timers[uniqueId]) {
clearTimeout (timers[uniqueId]);
}
timers[uniqueId] = setTimeout(callback, ms);
};
})();
//Usage:
$(window).resize(function () {
waitForFinalEvent(function(){
alert('Resize...');
//...
}, 500, "some unique string");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment