• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery delay – How to Delay the Start of a Method

jQuery delay – How to Delay the Start of a Method

April 3, 2022 Leave a Comment

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:


<div style="display: none;" id="div1">This div is currently hidden, but will be shown after a 5 second delay.</div>

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:


<style>.box{ display: none; height: 60px; width:60px; background: #7bbfa2; }</style>
<div id="div1">
  <div class="box"></div>
  <div id="click-me">Show div in 3 seconds</div>
</div>

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:


<style>.box{ display: none; height: 60px; width:60px; background: #7bbfa2; }</style>
<div id="div1">
  <div class="box"></div>
  <div id="click-me">Show div in 3 seconds</div>
</div>

<script>

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

</script>

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

Other Articles You'll Also Like:

  • 1.  Resizing an Image Using jQuery
  • 2.  How to Use jQuery to Clear a textarea Field
  • 3.  Using jQuery to Count Children of an Element
  • 4.  Using jQuery to Scroll to Top
  • 5.  How to Set All Checkbox to Checked in jQuery
  • 6.  Check if Input is Empty Using jQuery
  • 7.  Using jQuery to Get the Next Sibling of an Element
  • 8.  Using jQuery to Get the Top Position of Element
  • 9.  How to Use jQuery to Animate the Font Size of a Paragraph
  • 10.  jQuery Random Number Generator Between Two Numbers

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy