-3

We have a facts page on our website with the following code:

<div class='buttonplacer'>
<button class="facts-button1">Fun Fact #1</button> 
<button class="facts-button2">Fun Fact #2</button> 
<button class="facts-button3">Fun Fact #3</button> 
<button class="facts-button4">Fun Fact #4</button> 
<button class="facts-button5">Fun Fact #5</button> 
<button class="facts-button6">Fun Fact #6</button> 
</div>
</div>


<script> 

jQuery(".facts-button1").click(function(){
    jQuery('#fact-img').attr('src', '/img/backgrounds/fact1-img.png');
    jQuery("#fact1").removeClass("hide");
    jQuery("#fact-standaard").addClass("hide");
    jQuery("#fact2").addClass("hide");
    jQuery("#fact3").addClass("hide");
    jQuery("#fact4").addClass("hide");
    jQuery("#fact5").addClass("hide");
    jQuery("#fact6").addClass("hide");
});

jQuery(".facts-button2").click(function(){
   jQuery('#fact-img').attr('src', '/img/backgrounds/fact2-img.png');
    jQuery("#fact2").removeClass("hide");
    jQuery("#fact-standaard").addClass("hide");
    jQuery("#fact1").addClass("hide");
    jQuery("#fact3").addClass("hide");
    jQuery("#fact4").addClass("hide");
    jQuery("#fact5").addClass("hide");
    jQuery("#fact6").addClass("hide");
});

jQuery(".facts-button3").click(function(){
   jQuery('#fact-img').attr('src', '/img/backgrounds/fact3-img.png');
    jQuery("#fact3").removeClass("hide");
    jQuery("#fact-standaard").addClass("hide");
    jQuery("#fact2").addClass("hide");
    jQuery("#fact1").addClass("hide");
    jQuery("#fact4").addClass("hide");
    jQuery("#fact5").addClass("hide");
    jQuery("#fact6").addClass("hide");
});



jQuery(".facts-button4").click(function(){
   jQuery('#fact-img').attr('src', 'img/backgrounds/fact4-img.png');
    jQuery("#fact4").removeClass("hide");
    jQuery("#fact-standaard").addClass("hide");
    jQuery("#fact2").addClass("hide");
    jQuery("#fact3").addClass("hide");
    jQuery("#fact1").addClass("hide");
    jQuery("#fact5").addClass("hide");
    jQuery("#fact6").addClass("hide");
});

jQuery(".facts-button5").click(function(){
   jQuery('#fact-img').attr('src', '/img/backgrounds/fact5-img.png');
    jQuery("#fact5").removeClass("hide");
    jQuery("#fact-standaard").addClass("hide");
    jQuery("#fact2").addClass("hide");
    jQuery("#fact3").addClass("hide");
    jQuery("#fact4").addClass("hide");
    jQuery("#fact1").addClass("hide");
    jQuery("#fact6").addClass("hide");
});

jQuery(".facts-button6").click(function(){
   jQuery('#fact-img').attr('src', '/img/backgrounds/fact6-img.png');
    jQuery("#fact6").removeClass("hide");
    jQuery("#fact-standaard").addClass("hide");
    jQuery("#fact2").addClass("hide");
    jQuery("#fact3").addClass("hide");
    jQuery("#fact4").addClass("hide");
    jQuery("#fact5").addClass("hide");
    jQuery("#fact1").addClass("hide");
});




</script> 

  </div>

<div class="col-md-1"> </div>
<?php $lang_code = icl_object_id(15,'page', true, ICL_LANGUAGE_CODE); ?>

<div id="facts-right" class="col-md-4" >
    <div id="fact-standaard">
<?php the_field( "fact_1", $lang_code );  ?></div>  
 <div id="fact1" class="hide">
<?php the_field( "fact_1", $lang_code);  ?>
</div>
 <div id="fact2" class="hide">
<?php the_field( "fact_2", $lang_code );  ?>
</div>
 <div id="fact3" class="hide">
<?php the_field( "fact_3", $lang_code );  ?>
</div>
 <div id="fact4" class="hide">
<?php the_field( "fact_4", $lang_code );  ?>
</div>
 <div id="fact5" class="hide">
<?php the_field( "fact_5", $lang_code );  ?>
</div>
 <div id="fact6" class="hide">
<?php the_field( "fact_6", $lang_code );  ?>
</div>

What we would like to do is to make it a timed event. So that the facts change every 5 seconds, but still has the click funtion as it has now. Does anyone know a way to do this?

Sjoerd
  • 87
  • 2
  • 15
  • There is a very high level of redundancy in your code — you might want to look into fixing that to begin with. – Terry Mar 16 '15 at 11:07

2 Answers2

1

Use setInterval function for initiating a click on every 5 seconds

var i=1;
setInterval(function(){ 
jQuery(".facts-button"+i).click();
i++;
if(i==7)
 i=1; 
}, 5000);
Ankit Chaudhary
  • 4,059
  • 1
  • 11
  • 23
1

Try this like,

var arrId=["#fact1","#fact2","#fact3","#fact4"];// you can add more
var arrImg=["fact1-img.png","fact2-img.png","fact3-img.png","fact4-img.png"];

var counter=0;
var currentFact=1;// for selected fact id;
// set interval to do the task for above array variables
setInterval(function(){
    if(counter == arrId.length){
        counter =0; // reinit counter
    }
    jQuery("#fact-standaard").addClass("hide");
    for(var i=0,l=arrId.length;i<l;i++){
        if(currentFact==i){
            // for current fact, remove hide class and add image for it
            jQuery('#fact-img').attr('src', '/img/backgrounds/'+arrImg[i]);
            jQuery(arrId[i]).removeClass("hide");// remove the current facet id
        } else {
            jQuery(arrId[i]).addClass("hide");
        }
    }
    counter++;
},5000); // for five seconds delay

Also, try the short code to do the same,

jQuery(".facts-button2").click(function(){
   jQuery('#fact-img').attr('src', '/img/backgrounds/fact2-img.png');
    jQuery("#fact2").removeClass("hide");
    jQuery("#fact-standaard,#fact1,#fact3,#fact4,#fact5,#fact6").addClass("hide");
});
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106