Change background color javascript in specific order -


i'm new javascript, , need bit of help. want web page's background color changes #003372 #ffffff , #be2e37 in specific order (every 1000 milliseconds), without generating them @ random. reason why because made flash movie on website , want match colors appear.

javascript:

<script type="text/javascript">         function changebackground() {              var colors = ["#003372","#ffffff","#be2e37"];          setinterval(function() {              var bodybgarrayno = math.floor(math.random() * colors.length);             var selectedcolor = colors[bodybgarrayno];             document.body.style.background = selectedcolor;         }, 2000);     }     </script> 

html:

<body onload = changebackground();> 

try this:

function changebackground() {      var colors = ["#003372","#ffffff","#be2e37"], icolor = 0;      setinterval(function() {          if (icolor > (colors.length - 1)) icolor = 0;          var selectedcolor = colors[icolor];         document.body.style.background = selectedcolor;         console.log(icolor);         icolor += 1;      }, 1000); } 

you need declare , increment value of icolor in each interval , set 0 when reach end of array length

demo


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -