Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery delay() not delaying

Tags:

jquery

Why does this empty the text immediately (ignoring delay)?

$('#error_box_text').html('error text').delay(5000).html('')

#

jQuery 1.4

like image 840
artemave Avatar asked Mar 12 '10 12:03

artemave


People also ask

How to set delay in jQuery?

jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.

How do you make a delay in JavaScript?

The standard way of creating a delay in JavaScript is to use its setTimeout method. For example: console. log("Hello"); setTimeout(() => { console.

How do you delay a function?

To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.


2 Answers

delay will never delay regular methods - only those who are pushed to the animation/effect chain. If you want to delay your html() call, use queue ( http://api.jquery.com/queue/ ):

$('#error_box_text').html('error text').delay(5000).queue(function() {
   $(this).html('')
});

It would be nice if you could do

$('#error_box_text').html('error text').delay(5000, function() { $(this).html('') });

but this isn't possible (yet).

like image 72
Marcel Jackwerth Avatar answered Sep 22 '22 18:09

Marcel Jackwerth


Try

var sel = $('#error_box_text');
sel.html('error text');
setTimeout(function(){
    sel.html('');
}, 5000);

See delay()

jQuery.delay() is best for delaying between queued jQuery effects and such, and is not a replacement for JavaScript's native setTimeout function

like image 33
rahul Avatar answered Sep 22 '22 18:09

rahul