ivapopova

changeColorsOnInterval-Simple.html

Jun 9th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.68 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>TMP</title>
  5. </head>
  6. <body>
  7.  
  8.     <script type="text/javascript">
  9.         function changeBodyBG(color){
  10.             document.body.style.backgroundColor = color;
  11.         }
  12.  
  13.         function setNextColor(){
  14.             console.log(i)
  15.             changeBodyBG(colors[i]);
  16.  
  17.             // change index (you can do it with '%' as well)
  18.             if(i < colors.length-1){
  19.                 i++
  20.             }else{
  21.                 i = 0
  22.             }
  23.         }
  24.  
  25.         var colors = ["red", "green", "blue"];
  26.         var i  = 0;
  27.  
  28.         var id = setInterval(setNextColor, 1000)
  29.  
  30.         // Task: call the clearInterval when a "Stop" button is clicked. Sure, you have to put that button and attach event to it
  31.         // clearInterval(id)
  32.     </script>
  33. </body>
  34. </html>
Advertisement
Add Comment
Please, Sign In to add comment