How could I clear the field message after a couple seconds using delay jQuery function instead of setTimeout? The code below will clear value immediately.
var $this = $(this),
mid = $this.find("#emid"),
contents = $this.find("#equestion");
if(contents.length<30){
contents.css('color','red')
.val('Error Message')
.delay(5000)
.val('');
}
UPD: Actually, you can simply use the default queue and freely chain effects (like show) and non-effects (like val). Just wrap the latter in a queue-dequeue call:
$("input")
.queue(function() { $(this).val("blah").dequeue() })
.delay(1000)
.queue(function() { $(this).val("").dequeue() })
.fadeOut(1000) // etc
http://jsfiddle.net/cA4jB/1/
/UPD
No, you don't have to (and actually shouldn't) use setTimeout. jQuery provides a nice built-in mechanism for this, called queue. The basic idea is like this: you "collect" functions or delays in a named queue:
$(elem).queue("queueName", function(next) { do something and call next() });
$(elem).queue("queueName", function(next) { do something else and call next() });
$(elem).delay(3000, "queueName");
$(elem).queue("queueName", function(next) { do something else and call next() });
and then call dequeue() to start processing:
$(elem).dequeue("queueName")
Each function in the queue is called one after another, and delays work as expected.
In action: http://jsfiddle.net/cA4jB/
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