How to cycle the background-image for the body tag?

function changeBg() { $("body").css("background","red"); $("body").css("background","green"); } changeBg(); setInterval(changeBg, 3000); 

Closed due to the fact that the essence of the question is incomprehensible by the participants Alexey Shimansky , aleksandr barakin , cheops , user194374, Cerbo 26 Jun '16 at 21:02 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • I updated the question, I do it like this, the code does not work, tell me why? - asd
  • 2
    The code works. And it works exactly as you described it. The method executes the first line at the beginning. Everything is painted in red, and then the second is repainted in green. If you want to change - put the colors in the array, get another variable - the array index and add it every time in the function. And take the index of this array just with the help of this variable ... only as soon as the index reaches the limit of the array - it will be necessary to turn it on - Aleksey Shimansky

1 answer 1

 setInterval(changeBody, 3000); function changeBody() { $('body').css('background-color') == "rgb(255, 0, 0)" ? $('body').css('background', 'yellow') : $('body').css('background', 'red'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> 

 changeBody(); function changeBody(i) { var colors = ['red', 'green', 'blue', 'yellow']; if(typeof(i) == 'undefined' || i > colors.length) i = 0; $('body').css('background-color', colors[i]); setTimeout(changeBody, 2000, i+1); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script>