How to make the text change every 5 seconds? My code

setInterval(function() { $('.fot-kill-txt:contains("Всего убийств")').text("Всего смертей"); $('.fot-global-txt:contains("5")').text("15"); $('.fot-global-txt-you:contains("3")').text("16"); }, 5000); setInterval(function() { $('.fot-kill-txt:contains("Всего смертей")').text("Всего убийств животных"); $('.fot-global-txt:contains("15")').text("3"); $('.fot-global-txt-you:contains("16")').text("5"); }, 10000); setInterval(function() { $('.fot-kill-txt:contains("Всего убийств животных")').text("Всего убийств"); $('.fot-global-txt:contains("3")').text("9"); $('.fot-global-txt-you:contains("5")').text("2"); }, 12000); 
 <div class="fot-kill-txt">Всего убийств</div> <div class="fot-global-left-title">Глобальный</div> <div class="fot-global-txt">5 </div> <div class="fot-global-left-title">Вы</div> <div class="fot-global-txt-you">3</div> 

  • if every 5 minutes, then where is 12000? - Alexey Shimansky

1 answer 1

I suggest that you enter the necessary parameters either into an array or into an object with the required fields and already manipulate this data in the setInterval function.

Example:

 var values = [ ['Всего убийств', 5, 3], ['Всего смертей', 15, 16], ['Всего убийств животных', 3, 5] ]; var index = 1; var interval = 2000; // 2 secs. setInterval(function() { if (index > values.length - 1) index = 0; $('.fot-kill-txt').text(values[index][0]); $('.fot-global-txt').text(values[index][1]); $('.fot-global-txt-you').text(values[index][2]); index++; }, interval); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="fot-kill-txt">Всего убийств</div><br /> <div class="fot-global-left-title">Глобальный</div> <div class="fot-global-txt">5</div><br /> <div class="fot-global-left-title">Вы</div> <div class="fot-global-txt-you">3</div> 

Of course, it is better to bind the data to the keys (type values = {name: Всего убийств, death:3, animals: 5} ), because it is impossible to remember in which cell of the array what exactly is stored and the call, for example array[1][2] will not say anything about what is there and why. It should be remembered.

  • Thank you very much ... - Sauron
  • @Sauron If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Alexey Shimansky