Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing message jQuery delay setTimeout

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('');
      }
like image 798
Codex73 Avatar asked Sep 10 '26 15:09

Codex73


1 Answers

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/

like image 139
georg Avatar answered Sep 13 '26 05:09

georg