Question

Im trying to execute a block of code (will basically create a new input form and force the focus to that input) after a user finishes entering their 5 digit zip code. However, I want to create a small delay in between when the keyup event happens, and when the if statement does it's check. This will give users a chance to fix any mistakes they might be make and not be forced to immediately go to the next form.

This is what I have so far, I figure maybe use a .delay() function? But not sure where to put it, and haven't been able to come up with anything that works. I'm still learning jQuery and would greatly appreciate any help!

 $('input#zip').keyup(function(){
    if($('input#zip').val().length>=5){
             // do something
    }
 });

Also, I apologize in advanced if this has been answered. I did my best to search for the answer, but nothing was apparent to me as the solution.

Was it helpful?

Solution

You are looking for setTimeout()

 $('input#zip').keyup(function(){
    setTimeout(delayMyExecution(), 1000);
 });

function delayMyExecution(){
    if($('input#zip').val().length>=5){
             // do something
    }
}

Take a look here for some setTimeout() examples :

http://www.jquery4u.com/jquery-functions/settimeout-example/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top