I am messing around with jQuery’s AJAX functions and was trying to simulate how a real server would delay the otherwise smooth data requests that I get on local-host.
So I have written code that is similar to this:
$('form').submit(function (event) {
event.preventDefault();
$('#response').html('<p>Posting...</p>').fadeIn().delay(2000).queue(function () {
$.post (
'some_url.php',
{/*values here*/},
function (response) {
$('#response').html(response).delay(1000).fadeOut('slow');
//The line below is to reset the form element
$('input[type="text"], textarea').val(' ');
});
});
});
What I basically do here is I delay the $.post method by 2sec so that the “Posting…’ message can be seen.
When the 2sec are finished I want the text to be changed with the response I got, stay still for 1 sec and that I want it to fade out.
The first delay works perfect, also the Ajax call works perfectly, problem is – from some reason the second delay is not read and the response message, once shown, refuses to disappear 🙂
My question is why does this happen and how can I fix it?
You need to dequeue in order for the next thing in the queue to be processed.
http://api.jquery.com/jQuery.dequeue/
EDIT: Or
nextin 1.4 or greater:from: http://api.jquery.com/queue/