$("#date_1").hover( function () { var $this = $(this); $this.addClass("door"); setTimeout(function() { $this.addClass("doorstatic"); }, 2000);
You can group your classes as removeClass("door doorstatic")
The problem is that if you hover over it and doorstatic
for two seconds, you will still have the doorstatic
class for your element.
Correction:
$("#date_1").hover( function () { var $this = $(this), timer = $this.data("timer") || 0; clearTimeout(timer); $this.addClass("door"); timer = setTimeout(function() { $this.addClass("doorstatic"); }, 2000); // 2000 is in mil sec eq to 2 sec. $this.data("timer", timer); }, function () { var $this = $(this), timer = $this.data("timer") || 0; clearTimeout(timer); $this.removeClass("door doorstatic"); } );
Created a live example solution in jsFiddle .
source share