I have this
if(noDelay){
$(element).find("." + options.class).remove();
} else {
$(element).find("." + options.class).fadeOut().remove();
}
Is there a way I could avoid repeating the sentence and only add the fadeOut()
when a given condition is met?
I can't move fadeOut()
till the end of the chain, which probably would've made things easier.
I'm thinking something like
$(element).find("." + options.class).(if(noDelay) fadeOut()).remove();
Thanks in advance, Leo
There is nothing documented like you want but maybe this will work for you:
$(element).find("." + options.class).fadeOut(noDelay ? 0 : 400).remove();
Why 400, because the default duration for fadeOut is 400 milliseconds. (from documentation)
You could do this with a $.fn.each:
$(element).find("." + options.class).each(function(){
if (nodelay) {
$(this).remove();
}
else {
$(this).fadeOut().remove();
}
});
however it is far less efficient than simply doing what you already are doing.
Edit: here's another way to do it:
$(element).find("." + options.class)[ noDelay ? "detach" : "fadeOut" ]().remove();
basically, if noDelay is true, it will detach the elements before removing, else, it will fade them out before removing. Should be just as efficient as your code, just on 1 line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With