Created
February 15, 2019 12:19
-
-
Save imelgrat/5220279d7612715c1fde49c3cbac946d to your computer and use it in GitHub Desktop.
Debouncing JavaScript events to prevent jerky action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Debounce window-resize events | |
* | |
* Debouncing JavaScript events to prevent jerky action during UI changes. | |
* | |
* @link https://imelgrat.me/javascript/debouncing-javascript-events/ | |
*/ | |
$(window).bind('resize', function(e) | |
{ | |
window.resizeEvt; | |
$(window).resize(function() | |
{ | |
clearTimeout(window.resizeEvt); | |
window.resizeEvt = setTimeout(function() | |
{ | |
if($(window).width() != previous_width) | |
{ | |
//Do your stuff here | |
console.log('Resized finished.'); | |
} | |
}, 250); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment