Skip to content
Advertisement

jQuery fadeIn() called repeatedly not working

This is a part of an JS-scanning function which runs about 3-5 times / sec.

Below is what I want to obtain .. but it (obviosly) does not work … because it keeps “resetting” the fadeIn function 3-5 times every second.

if (scanResult) {
     dosomething();
} else {
     // Show error message
     $("#error").fadeIn().delay(3000).fadeOut();
}

I could call a seperate function and/or checks with timestamps when it last was called.

But … there must be a smarter jQuery-way?

Advertisement

Answer

Quick and dirty.

if (scanResult) {
     dosomething();
} else {
     // Show error message
     if(!$("#error").hasClass("shown"))
         $("#error").addClass("shown").fadeIn().delay(3000).fadeOut(() => {removeClass("shown");});
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement