title: Why does my function fail intermittently?
body: <p>I need some help with this script. Can't find out why it fails sometimes.
  It is working most of the time, but sometimes it's just showing blank page.</p>
  <p>This is the script (in the folders I have 3 image files named 1.jpg, 2.jpg and 3.jpg.:</p>
  <pre><code>function chgbg() {
      var d = new Date();
      var h = d.getHours();
      var n = d.getDay();
      var totalCount = 3;
      var num = Math.ceil(Math.random() * totalCount);
  
      /* Monday - Friday */
      if (n &gt;= 1 &amp;&amp; n &lt; 5 &amp;&amp; h &gt;= 23 &amp;&amp; h &lt; 6) {
          document.body.style.backgroundColor = &quot;#FFFFFF&quot;;
          document.body.style.backgroundImage = 'url(gfx/weekdays/00-06/' + num + '.jpg)';
      }
      if (n &gt;= 1 &amp;&amp; n &lt; 5 &amp;&amp; h &gt;= 6 &amp;&amp; h &lt; 11) {
          document.body.style.backgroundColor = &quot;#FFFFFF&quot;;
          document.body.style.backgroundImage = 'url(gfx/weekdays/06-11/' + num + '.jpg)';
      }
      if (n &gt;= 1 &amp;&amp; n &lt; 5 &amp;&amp; h &gt;= 11 &amp;&amp; h &lt; 14) {
          document.body.style.backgroundColor = &quot;#FFFFFF&quot;;
          document.body.style.backgroundImage = 'url(gfx/weekdays/11-14/' + num + '.jpg)';
      }
      if (n &gt;= 1 &amp;&amp; n &lt; 5 &amp;&amp; h &gt;= 14 &amp;&amp; h &lt; 18) {
          document.body.style.backgroundColor = &quot;#FFFFFF&quot;;
          document.body.style.backgroundImage = 'url(gfx/weekdays/14-18/' + num + '.jpg)';
      }
      if (n &gt;= 1 &amp;&amp; n &lt; 5 &amp;&amp; h &gt;= 18 &amp;&amp; h &lt; 23) {
          document.body.style.backgroundColor = &quot;#FFFFFF&quot;;
          document.body.style.backgroundImage = 'url(gfx/weekdays/18-00/' + num + '.jpg)';
      }
  
      /* Monday - Friday */
  </code></pre>