Automatically loading a div using jQuery after a delay of 5 seconds

I am working on a feature for my homepage that involves 4 div bars. Right now, I have them set to load upon click, but I would like to adjust it so that the first div loads when the page initially loads, and then each subsequent div loads after a certain time delay. Here is the current code I am using:

$(document).ready(function () {

     $('#click1').click(function () {
        $('#desc1').toggle(400);
        $('#desc2').hide();
        $('#desc3').hide();
        $('#desc4').hide();
        $('#desc5').hide();
     });
});

Thank you,

Answer №1

var countdown=5000;

    $(document).ready(function () {
            $('#step1').toggle(function(){
                $('#step2').toggle(countdown,function(){
                    $('#step3').toggle(countdown,function(){
                        $('#step4').toggle(countdown,function(){
                            $('#step5').toggle(countdown,function(){
                            });
                        });
                    });
                });
            });
        });    

Give this a shot!

Answer №2

To reveal them gradually, use delay(ms);

$('#desc2').delay(5000).show();
$('#desc3').delay(10000).show();
$('#desc4').delay(15000).show();
$('#desc5').delay(20000).show();

Answer №3

If you're looking for a neat solution, I recommend using the setInterval(); method.

Here's an example:

$('#button1').click(function () {
    var number = 1;
    var interval = setInterval( function(){                      
        var element = $('#description'+ number);
        element.addClass('display');
        number++;
        if( number > 6) {
            clearInterval(interval);
        }
    }, 300);
});

I found this snippet in one of my previous projects. Just a heads up, I use CSS to toggle the visibility of the div.

Answer №4

Give this script a shot

$('#btn1').on('click', function () {
        $('#about1').fadeIn(400);
        $('#about2').delay(5000).fadeIn();
        $('#about3').delay(10000).fadeIn();
        $('#about4').delay(15000).fadeIn();
        $('#about5').delay(20000).fadeIn();
     })

Answer №5

(function(m){ var x = 2, flag, link;

  function download(){

    if( x == 6){
      console.log("download complete!");
      clearTimeout(flag);
      return ;
    }

    //perform action
    $(selector+x).load(link+"&index="+x, function(){
      x++;
      flag = setTimeout(download,5000);
    })

  }
  download();
})('desc');

toggle can be used to show or hide content dynamically, I prefer using load and setTimeout for this purpose.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using a 90% zoom level in the browser does not trigger any media queries

When addressing the width of a specific class, I utilized CSS media queries as shown below: @media (max-width:1920px) { .slides { width: 860px; } } @media (max-width:1500px) { .slides { width: 852px; } } @media (max-width:1 ...

What is the best way to display loading details during a data loading process within a useEffect hook?

Whenever a specific custom React component I've created is initially mounted, it utilizes useEffect to initiate a lengthy multistep process of loading data that will later be rendered. Since the component isn't always rendered, this costly proces ...

The creation of numerous DOM elements in D3

I'm currently utilizing the Tableau JS API to develop animated charts with D3.js. Within a Canvas(DOM) element, I am generating the necessary charts using the getUnderlyingData() function. This function is triggered whenever there's a change in f ...

Having difficulty validating the field accurately with Angular.js

In order to validate the input field in accordance with the user's needs using AngularJS, I have shared my code below: <div ng-class="{ 'myError': billdata.longitude.$touched && billdata.longitude.$invalid }"> <input type ...

Comet: showcase nested object on the client side using handlebars

Received JSON from helper : { "perms": [ { "userId": "rA5s5jSz7q9ZSCcNJ", "perms": [ { "moduleName": "Gallery", "container": { "ImageUpload": { ...

Laravel modal form submission with verification

Recently, I started working with laravel and I'm struggling to manage laravel, ajax, and jquery. Currently, I am trying to insert data into the database. I have succeeded in doing so, but I encountered an issue with data validation. I want to validate ...

The initial element within the div style is malfunctioning

Could someone assist me in understanding why the first-of-type CSS is not working correctly? .item:first-of-type .delete{ display: none ; } .delete { text-decoration: none; color: red; padding-top: 40px;} .add_form_field { white-space: nowrap; } < ...

How come my Django application is returning an empty response when using jQuery.ajax?

Trying to send a JSON response from a Django view using an ajax call: var tab = 'example'; var response = $.ajax({ url: "/" + tab + "/" }).responseText; alert(response); This is my Django view code: If request.is_ajax() == True: req = ...

Retrieve the values from multiple columns within a jqgrid row

Is it possible to retrieve both the first column value and the second column value from a jqgrid row? In my other program, I am currently using this code to obtain the first value of the row: $("#tblTallySheet").jqGrid('getGridParam', 'selr ...

There seems to be an issue with the functionality of the `Nav` component in React-bootstrap when used within a `NavBar` with the

Desiring my NavBar to occupy the entire available width, I included a fill flag within the Nav section of the NavBar component. <Navbar bg="light" expand="lg"> <Navbar.Toggle aria-controls="basic-navbar-nav" /&g ...

Ways to disable an element

How can I prevent a <form:input type="text" path="lateTimeValue" disabled="true" id="lateTime" /> element from sending its value to the server when disabled? What steps should I take to ensure the value is not passed to the server upon submissio ...

Embarking on the journey of transitioning code from server-side to client-side

Currently, I am looking to transition the code behind section of my asp.net web forms application to client-side ajax or javascript - still deciding on which route to take. The main goal for this change is to ensure that the application remains functional ...

Angular.js can efficiently handle waiting for multiple resource calls and AJAX requests

I'm facing a challenge in my Angular.js application where I need to make three separate resource calls and then use the data together once all the requests are complete. Here are the three calls I need to make: # Retrieve the curriculum $scope.curric ...

Placing the copyright footer at the bottom of an Angular / C# application with Bootstrap 4 styling

I am facing troubles while attempting to utilize the sticky-footer-wrapper feature from Bootstrap in order to keep the footer fixed at the bottom of the page. However, instead of staying at the bottom, it just appears right after the content on the page fi ...

A guide on accessing every element within a "div" tag that begins with a specified text

In my HTML document, there is a div element present. I am looking to retrieve all elements within this specific div that have id attributes beginning with a certain string (e.g. "q17_"). Is it possible to accomplish this task using JavaScript? If necess ...

Pressing the submit button will trigger the execution of a .php script, which will then generate a popup on the screen and refresh a specific part of

I have a select form and submit button on my page, which are dynamically generated based on entries in the database. Here is the HTML output: <div id="structures"> <h1>Build</h1> <form name="buildForm" id="buildForm" method="POST" ons ...

displaying empty page using react router

I can't seem to figure out why my render is resulting in a blank page. I've searched through similar questions for a solution but haven't had any luck so far. Any help would be greatly appreciated! Here's my code: App.js import log ...

"Using jQuery to target elements in the HTML Document Object Model

Is there a way to retrieve the outer HTML of a selected object using jQuery? For instance, if I have: $("#test.test_class") how can I convert it into an HTML string like this: <div id="test" class="test_class"></div> If anyone knows how to ...

In the world of coding, the trio of javascript, $.ajax,

I need help with iterating over an array and assigning a variable using a for loop. Here is the scenario: function Person(name, status){ this.name = name; this.status = status; } var status = []; var array = ["bill","bob","carl","ton"]; function exAj ...

Interpret the JSON reply

Could someone please explain why my function B() is not responding? <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/ja ...