Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery delay the execution of code

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) ;
like image 796
user1077300 Avatar asked Sep 26 '13 11:09

user1077300


People also ask

Is delay () A event method in jQuery?

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.

What is the use of delay () method 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 wait 1 sec in JavaScript?

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.

How do I call a function after 2 seconds in jQuery?

To call a jQuery function after a certain delay, use the siteTimeout() method. Here, jQuery fadeOut() function is called after some seconds.


2 Answers

Set a timeout like this :

var delay = 1000;
setTimeout(function() {
 // your code
}, delay);

Example http://jsfiddle.net/HuLTs/

like image 111
Paul Rad Avatar answered Oct 05 '22 07:10

Paul Rad


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/

like image 21
GG. Avatar answered Oct 05 '22 06:10

GG.