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

1
  • 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: stackoverflow.com/questions/7310109/… Commented Sep 27, 2017 at 4:05

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.