We can use jQuery to delay the start of certain methods by using the jQuery delay() method.

$('#div1').delay(2000).fadeIn();

In the example above, we use the jQuery delay method to delay the showing of the div, #div1.

You will also notice the number 2000 in the delay() method. That is the number of milliseconds to delay the next method. So in this case, the delay will be 2 seconds. We can change this number to be however long we want to delay the next method call.


Let’s say we have the following HTML code:



If we want to show the hidden div, #div1, after a 5-second delay from page load, we can use the following jQuery code.

$('#div1').delay(5000).fadeIn();

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery('#div1').delay(5000).fadeIn();

Note that we could also do this by using the setTimeout() method. Here is a quick code snippet to show how it would be done.

setTimeout(function delayStart() {
  $('#div1').fadeIn();
}, 5000);

Using jQuery to Delay the Start on a Click

In this example, we will have a div that will be a box. The box will be hidden to start, and we will let the user click a button to show the box after a 3-second delay.

Here is the simple HTML set up:



Show div in 3 seconds

In the JavaScript code, when a user clicks the button, we will run a function that we will create.

The function will simply show the box div after a 3-second delay. One problem with the jQuery delay method is that it does not work on all method calls, only ones that can be queued up, like the fadeIn() method. If we want to use it on say the jQuery show() method it will not work.

Because of this, we much prefer to use the setTimeout() method as we did above. So we will use that code for this example below as well.

Here is the jQuery and JavaScript code:

$('#click-me').click(function(){
  setTimeout(function delayStart() {
    $('.box').show();
  }, 3000);
});

The final code and output for this example is below:

Code Output:

Show div in 3 seconds

Full Code:



Show div in 3 seconds

Hopefully this article has helped you understand how the jQuery delay method works.

Categorized in:

jQuery,

Last Update: February 26, 2024