In my jquery function I have a loader gif image. After I show it I want to put a delay for a second and then continue to execute the rest of the code. How can I do that?
$('#loader').css('display', '');
//// I want to put here a delay.
var myDate = new Date();
myDate.setFullYear(2013,8,2);
var checkyear = myDate.getFullYear();
var monthly =myDate.getMonth();
var daily =myDate.getDate();
$('#day').html(daily) ;
$('#month').html(months[monthly]) ;
$('#year').html(checkyear) ;
The delay() is an inbuilt method in jQuery which is used to set a timer to delay the execution of the next item in the queue.
jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.
To delay a function execution in JavaScript by 1 second, wrap a promise execution inside a function and wrap the Promise's resolve() in a setTimeout() as shown below. setTimeout() accepts time in milliseconds, so setTimeout(fn, 1000) tells JavaScript to call fn after 1 second.
To call a jQuery function after a certain delay, use the siteTimeout() method. Here, jQuery fadeOut() function is called after some seconds.
Set a timeout like this :
var delay = 1000;
setTimeout(function() {
// your code
}, delay);
Example http://jsfiddle.net/HuLTs/
Have you tried .delay ?
$('#loader').show(1).delay(1000).hide(1);
The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.
Demo : http://jsfiddle.net/SBrWa/
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