-2

I'm trying to make an automatic slideshow using setInterval in js. In google chrome and android it works great, but I can't make it work on IE, IOS and Mozilla firefox. Here is my javascript:

/*start loop animation*/
autoplay()

function autoplay(){
setInterval(() => {
  $('#page2').trigger('click');
 }, 5000);

 setInterval(() => {
  $('#page3').trigger('click');
 }, 10000);

 setInterval(() => {
  $('#page4').trigger('click');
 }, 15000);

 setInterval(() => {
  $('#page5').trigger('click');
 }, 20000);

 setInterval(() => {
  $('#page6').trigger('click');
 }, 25000);

 setInterval(() => {
  $('#page7').trigger('click');
 }, 30000);

 setInterval(() => {
  $('#page8').trigger('click');
 }, 35000);

 setInterval(() => {
  $('#page9').trigger('click');
 }, 40000);

 setInterval(() => {
  $('#page10').trigger('click');
 }, 45000);

 setInterval(() => {
  $('#page11').trigger('click');
 }, 50000);

 setInterval(() => {
  $('#page1').trigger('click');
 }, 55000);
}

Thank you for all of your help

  • Possible problem: The click must come from a user, this means a "click" event can't be fired from a `setInterval`, can you check and swap one `setInterval` with a `$(".btn").click` and see what happens when you hit the button ? – Marc Jan 05 '20 at 22:46

1 Answers1

0

Keep your code DRY. Try making this a loop.

var i = 0;
var pageNum = 0;
var intervalId = setInterval(function(){
   if(i === 55000){
      clearInterval(intervalId);
   }
   //increment pageNumber and trigger page change
   pageNum++;
   i += 5000
}, 5000);
adlopez15
  • 3,449
  • 2
  • 14
  • 19
  • Ah, thanks for noting that. I reread and see I missed the issue. There is an answer that was posted here that might be helpful: https://stackoverflow.com/questions/1421584/how-can-i-simulate-a-click-to-an-anchor-tag/1421968#1421968 However, why not use: `document.getElementById('elementID').click();` – adlopez15 Jan 05 '20 at 22:52