I have the ajaxloader show and hide in my ajaxsetup like this
beforeSend:function(xhr, settings){
$('.loader').show();
complete:function(){
$('.loader').hide();
Now the request happens very fast and my animation only loads for merely less than second.
Is there any way i can put some delay so that animation stays there for 3 seconds and then div gets updated.
So basically its delay the ajax call not just delaying the animation because i also dont wnat that ajax request gets completed and animation is still there
Since it has never been crystal clear to me what exactly you’re trying to do, here is a solution that does the following:
Here’s the code:
Caveat – this will work as long as no call to
$.ajax()was expecting it to return the jqXHR object and there is no use of ajax deferreds. If those are used, then a more complicated solution would need to be coded that holds off the success handler and completion handler.Edit: these are older attempts to understand what the question was:
To make sure the notification is on screen for a minimum amount of time, you could do this:
This will keep track of how long the message was on screen and if the ajax call went quickly, it will delay hiding the msg until 3 seconds have elapsed. If the ajax call didn’t go quickly and the message has already been displayed long enough, it will just hide it when the ajax call completes.
If what you want to do is to delay the loading of the content and you are processing the result in the
completehandler, then you can do that like this:If you are not processing the result of the ajax all in the complete handler, then please include the code for processing the result so I can include it in this idea. The general idea here is that you record the time when the ajax call starts. If when the ajax response comes, it has been less than 3 seconds, you set a timer for the amount of remaining time in the 3 seconds and finish the response in the timer callback. If it’s been more than 3 seconds, then you just go ahead and process the result normally.
This allows you to never use more time than desired, but always at least 3 seconds. You could just delay launching the ajax call for 3 seconds, but then if you ajax call takes 6 seconds to get a result, the whole process would take 9 seconds which is not what you want. In my scheme, if the ajax result takes 1 second, you will delay processing it until 3 seconds. If the ajax result takes 6 seconds, the result will get processed right when it comes in (no extra 3 second delay).