Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show and hide divs at a specific time interval using jQuery

I would like to show divs at a specific interval (10 seconds) and show next div and as go on and repeat the same.

**

Sequence :

** On 10th second show div1 , hide other divs ,
After 5seconds interval Show div 2 and hide other divs,
After 5 seconds interval Show div 3 and hide other divs,
and repeat the same for every 10 seconds.


Code Follows:


<div id='div1' style="display:none;">    <!-- content --> </div>  <div id='div2' style="display:none;">    <!-- content --> </div>  <div id='div3' style="display:none;">    <!-- content --> </div> 
like image 269
Webrsk Avatar asked May 27 '09 10:05

Webrsk


People also ask

How do you show div for 10 seconds and then hide it jQuery?

Given a div element and the task is to hide the div element after few seconds using jQuery ? Approach: Select the div element. Use delay function (setTimeOut(), delay()) to provide the delay to hide() the div element.

How do I hide a specific div?

We hide the divs by adding a CSS class called hidden to the outer div called . text_container . This will trigger CSS to hide the inner div.

Which function is used to show and hide an element on a certain action in jQuery?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.

Can be used in jQuery to hide and display an element of HTML?

jQuery toggle() You can also toggle between hiding and showing an element with the toggle() method.


2 Answers

Working Example here - add /edit to the URL to play with the code

You just need to use JavaScript setInterval function

$('html').addClass('js');    $(function() {      var timer = setInterval(showDiv, 5000);      var counter = 0;      function showDiv() {      if (counter == 0) {        counter++;        return;      }        $('div', '#container')        .stop()        .hide()        .filter(function() {          return this.id.match('div' + counter);        })        .show('fast');      counter == 3 ? counter = 0 : counter++;      }    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">    <head>    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>    <title>Sandbox</title>    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />    <style type="text/css" media="screen">      body {        background-color: #fff;        font: 16px Helvetica, Arial;        color: #000;      }            .display {        width: 300px;        height: 200px;        border: 2px solid #000;      }            .js .display {        display: none;      }    </style>  </head>    <body>    <h2>Example of using setInterval to trigger display of Div</h2>    <p>The first div will display after 10 seconds...</p>    <div id='container'>      <div id='div1' class='display' style="background-color: red;">        div1      </div>      <div id='div2' class='display' style="background-color: green;">        div2      </div>      <div id='div3' class='display' style="background-color: blue;">        div3      </div>      <div>  </body>    </html>

EDIT:

In response to your comment about the container div, just modify this

$('div','#container') 

to this

$('#div1, #div2, #div3') 
like image 160
Russ Cam Avatar answered Oct 09 '22 19:10

Russ Cam


Loop through divs every 10 seconds.

$(function () {      var counter = 0,         divs = $('#div1, #div2, #div3');      function showDiv () {         divs.hide() // hide all divs             .filter(function (index) { return index == counter % 3; }) // figure out correct div to show             .show('fast'); // and show it          counter++;     }; // function to loop through divs and show correct div      showDiv(); // show first div          setInterval(function () {         showDiv(); // show next div     }, 10 * 1000); // do this every 10 seconds      }); 
like image 25
Alexander Ulizko Avatar answered Oct 09 '22 20:10

Alexander Ulizko