0

I need to loop through the function switch_tiles so the function will run as switch_tiles(1); switch_tiles(2); etc... but it needs to be i++ every 5 seconds. I've tried putting the interval inside the loop but that didn't help either. Also, after i = 5 I want it to reset.

window.setInterval(function(){
 for(var i = 1; i < 5; i++){
   switch_tiles(i);
 }
}, 5000);

This is all the function does so that's not too important to the question. Just added for context.

function switch_tiles(n){
  var last = $('.active').attr('id');
  $('#'+last).removeClass('fas');
  $('#'+last).removeClass('active');
  $('#'+last).addClass('far');

  $('.active_tile').fadeOut();
  $('#tile_' + n).fadeIn();

  $('#circle_' + n).removeClass('far');
  $('#circle_' + n).addClass('fas');
  $('#circle_' + n).addClass('active');
}
Reece
  • 35
  • 6

4 Answers4

1

Try this:

var i = 1;
function myFunction() {
  if (i > 5)
    i = 1;

  switch_tiles(i++);
  window.setTimeout(myFunction, 5000);      
}
myFunction();
Majid Akbari
  • 200
  • 12
  • 1
    This fails to start over at 1 again. – gforce301 Aug 08 '18 at 20:12
  • It is working!!! Here is a fiddle: https://jsfiddle.net/xpvt214o/574750/ – Majid Akbari Aug 08 '18 at 20:16
  • No, it's not working. What part of "It fails to start over again at 1" was not clear enough? – gforce301 Aug 08 '18 at 20:18
  • It is quite simple! Just set the 'i' to 0 after the 5th run. Are you just happy to give negative feedback to people answers?! – Majid Akbari Aug 08 '18 at 20:22
  • Really man. You posted an answer that does not do what the OP asked it to do. Are you really just happy to try to defend your non-working answer instead of just fixing it? – gforce301 Aug 08 '18 at 20:24
  • I am just saying that the main functionality is done and it just needs a line to complete the task. Does it deserve a negative feedback? By the way, sorry buddy. Thanks for the feedback, I have edited the answer. – Majid Akbari Aug 08 '18 at 20:35
  • 1
    Yes it deserved "negative feedback" if pointing out that your answer doesn't do what the OP wanted is considered "negative". The reason it deserved it? Other less knowledgeable people will come to this post looking for an answer. If they chose yours they would be left wondering why it doesn't work, how to fix it and this would just generate another question when instead you could fix your answer or remove it. Either one works. thanks for fixing it. – gforce301 Aug 08 '18 at 20:41
0
var i = 1;
window.setInterval(function(){
   if (i == 5){
      //reset over here.
   }
   switch_tiles(i);
   i++;
}, 5000);
full-stack
  • 553
  • 5
  • 20
  • This is exactly what I needed, I changed it though so i = 2 at the start as it was already on tile 1 which was a mistake on my part but thank you so much! I'll accept the answer when it allows me to – Reece Aug 08 '18 at 20:14
0
i = 0;
window.setInterval(function() {
  i++;
  if(i > 5){
    i = 1;
  } 
  switch_tiles(i)
}, 5000);

You just need to tell i to be whatever number you want at any time. Also, this all depends on if you have your tiles at base-0 or base-1. You would put your i++ before or after the switch_tiles()

ntgCleaner
  • 5,865
  • 9
  • 48
  • 86
0

What you want is probably something like that:

window.setInterval(function(){
  switch_tiles(i++);
  if (i>5) i=0;
  }, 5000);

This will run the function every 5 sec, increasing the parameter every time.

Note that this uses a global variable window.i so it's sort of dirty...

full-stack
  • 553
  • 5
  • 20
iPirat
  • 2,197
  • 1
  • 17
  • 30