title: How to create a looping html changing background image with javascript
body: <p>My JavaScript script, which is meant to continually change the background image of my HTML page, will not do what it is supposed to.</p>
  
  <pre><code>function CB() {
  try {
      var p = {
        Pic1: 'Images/CoolPic2.jpg',
        Pic2: 'Images/CoolPic3.jpg',
        Pic3: 'Images/CoolPic4.jpg'
     };
     var l = p.length;
      i = 0;
      while (i &lt; l) {
          setTimeout(function(){document.body.style.backgroundImage = url(p[i]); }, 3000)
          i += 1;
          if (i &gt; l) {
              i -= l;
              }
      }
  }
  catch(err) {
      alert(err.message);
  }
  }
  </code></pre>