Skip to content

Instantly share code, notes, and snippets.

@imelgrat
Created February 15, 2019 12:19
Show Gist options
  • Save imelgrat/5220279d7612715c1fde49c3cbac946d to your computer and use it in GitHub Desktop.
Save imelgrat/5220279d7612715c1fde49c3cbac946d to your computer and use it in GitHub Desktop.
Debouncing JavaScript events to prevent jerky action
/**
* 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