Skip to content

Instantly share code, notes, and snippets.

@yairEO
Created October 3, 2017 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yairEO/d607ac7a53d2aa87aa7e67fe13b8f9d6 to your computer and use it in GitHub Desktop.
Save yairEO/d607ac7a53d2aa87aa7e67fe13b8f9d6 to your computer and use it in GitHub Desktop.
add temporary class
$.fn.addTempClass = function(tempClass, duration){
if( !tempClass )
return this;
return this.each(function(){
var $elm = $(this),
timer;
$elm.addClass(tempClass);
// clear any timeout, if was any
clearTimeout( $elm.data('timeout') )
timer = setTimeout(function(){
$elm.removeClass(tempClass).removeData('timeout');
}, duration || 100);
// set the timer on the element
$elm.data('timeout', timer);
});
};
@yairEO
Copy link
Author

yairEO commented Oct 3, 2017

usage:

$(document.body).addTempClass('some_class', 2000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment