css - Jquery div background image cycle -


any idea why code broken or not working? not code appear correct, of course missing obvious. need background image of div cycle new 1 in the/a array ever 5 seconds.

var imageindex = 0; var imagesarray = new array();  //set images imagesarray[0] = "images/self-whitewater.png"; imagesarray[1] = "images/fishing.png"; imagesarray[2] = "images/solo-kayaking.png";  function changebackground(){ $("main-blocki").css("background","url('"+ imagesarray[imageindex] +"')");                             imageindex++;     if (imageindex > imagearray.length)         imageindex = 0;  }  $(document).ready(function() {   setinterval("changebackground()",5000); }); 

your problem in if statement, specified @thatidiotguy.

but in oneliner, without if statement.

var imageindex = 0; var imagesarray = [     "images/self-whitewater.png",     "images/fishing.png",     "images/solo-kayaking.png" ];  function changebackground(){     var index = imageindex++ % imagesarray.length;     $("main-blocki").css("background","url('"+ imagesarray[index] +"')");       }  $(document).ready(function() {   setinterval(changebackground, 5000); }); 

notice imageindex++ % imagesarray.length. increments global imageindex while making sure value not greater imagesarray.length.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -