Why does this empty the text immediately (ignoring delay)?
$('#error_box_text').html('error text').delay(5000).html('')
jQuery 1.4
jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.
The standard way of creating a delay in JavaScript is to use its setTimeout method. For example: console. log("Hello"); setTimeout(() => { console.
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.
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).
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
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