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>
This question is related to
javascript
jquery
css
html
timeout
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
});
See InnerFade.
<script type="text/javascript">
$(document).ready(
function() {
$('#portfolio').innerfade({
speed: 'slow',
timeout: 10000,
type: 'sequence',
containerheight: '220px'
});
});
</script>
<ul id="portfolio">
<li>
<a href="http://medienfreunde.com/deutsch/referenzen/kreation/good_guy__bad_guy.html">
<img src="images/ggbg.gif" alt="Good Guy bad Guy" />
</a>
</li>
<li>
<a href="http://medienfreunde.com/deutsch/referenzen/kreation/whizzkids.html">
<img src="images/whizzkids.gif" alt="Whizzkids" />
</a>
</li>
<li>
<a href="http://medienfreunde.com/deutsch/referenzen/printdesign/koenigin_mutter.html">
<img src="images/km.jpg" alt="Königin Mutter" />
</a>
</li>
<li>
<a href="http://medienfreunde.com/deutsch/referenzen/webdesign/rt_reprotechnik_-_hybride_archivierung.html">
<img src="images/rt_arch.jpg" alt="RT Hybride Archivierung" />
</a>
</li>
<li>
<a href="http://medienfreunde.com/deutsch/referenzen/kommunikation/tuev_sued_gruppe.html">
<img src="images/tuev.jpg" alt="TÜV SÜD Gruppe" />
</a>
</li>
</ul>
here is a jQuery plugin I came up with:
$.fn.cycle = function(timeout){
var $all_elem = $(this)
show_cycle_elem = function(index){
if(index == $all_elem.length) return; //you can make it start-over, if you want
$all_elem.hide().eq(index).fadeIn()
setTimeout(function(){show_cycle_elem(++index)}, timeout);
}
show_cycle_elem(0);
}
You need to have a common classname for all the divs you wan to cycle, use it like this:
$("div.cycleme").cycle(5000)
Heres a another take on this problem, using recursion and without using mutable variables. Also, im not using setInterval
so theres no cleanup that has to be done.
Having this HTML
<section id="testimonials">
<h2>My testimonial spinner</h2>
<div class="testimonial">
<p>First content</p>
</div>
<div class="testimonial">
<p>Second content</p>
</div>
<div class="testimonial">
<p>Third content</p>
</div>
</section>
Using ES2016
Here you call the function recursively and update the arguments.
const testimonials = $('#testimonials')
.children()
.filter('div.testimonial');
const showTestimonial = index => {
testimonials.hide();
$(testimonials[index]).fadeIn();
return index === testimonials.length
? showTestimonial(0)
: setTimeout(() => { showTestimonial(index + 1); }, 10000);
}
showTestimonial(0); // id of the first element you want to show.
Try this
$('document').ready(function(){
window.setTimeout('test()',time in milliseconds);
});
function test(){
$('#divid').hide();
}
Source: Stackoverflow.com