ivapopova

changeColorsOnTimeout-Simple.html

Jun 9th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 0.67 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.         period = 1000
  28.  
  29.         for (var j = 0; j < colors.length; j++){
  30.             // TASK: why we do not pass j as argument to setNextColor?
  31.             setTimeout( setNextColor, period )
  32.             period+=1000
  33.         }
  34.     </script>
  35. </body>
  36. </html>
Advertisement
Add Comment
Please, Sign In to add comment