2

I would like to add a delay of 2 seconds to the following code:

if (length < 20 & warningContainer.is(\':empty\')){
warningContainer.html(minLengthMessage);
el.addClass(\'input-error\');
}

How do I do that?

I tried:

warningContainer.delay(2000).html(minLengthMessage);

PS: I have no clue how to code JQuery

Thank you

stylusss
  • 79
  • 2
  • 10
  • jQuery delay works on effects only. So use JavaScript's built in `setTimeout`. Also I have noticed that you are using bitwise AND instead of logical AND inside the if statement. You can read more about it here: https://stackoverflow.com/questions/7310109/whats-the-difference-between-and-in-javascript – sbr Sep 27 '17 at 04:05

2 Answers2

4

When you call delay(2000), what you’re actually doing is calling delay(2000, "fx") as the function has an optional second parameter (the queue name you wish to delay operation of) which defaults to fx. The various effects methods (fadeOut(), animate() etc.) all use this queue which is why delay() has an effect on them.

I think you can use

setTimeout(function(){
    // add your code
}, 2000);

https://www.mattlunn.me.uk/blog/2012/06/jquery-delay-not-working-for-you/

2

I am not sure myself how the .delay works in jQuery but you can do this the old JS way.

setTimeout(function() {
    warningContainer.delay(2000).html(minLengthMessage);
}, 2000);
Junaid
  • 1,270
  • 3
  • 14
  • 33