Skip to content

Instantly share code, notes, and snippets.

@franklinjavier
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save franklinjavier/e2da8dd833f0f342c348 to your computer and use it in GitHub Desktop.

Select an option

Save franklinjavier/e2da8dd833f0f342c348 to your computer and use it in GitHub Desktop.
function debounce( e, callback, ms ) {
switch( e.type ) {
case 'mouseover':
try {
window.timeoutId = setTimeout( callback, ms );
} catch( evt ) {
console.error( evt );
}
break;
case 'mouseout':
if ( window.timeoutId ) {
clearTimeout( window.timeoutId );
return false;
}
break;
}
}
<div></div>
<div></div>
<div></div>
<div></div>
function myCallback() {
  console.log('beep boop');
}

$('div')
.on('mouseover', function(e){ 
  debounce( e, myCallback, 1000 ); 
})
.on('mouseout', function(e){ 
  debounce( e ); 
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment