title: How to make an infinite loop with delay in JavaScript?
body: <p>I'm trying to make div which could change it color. Here is my code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>window.onload = function () {
      for (;;){
          setTimeout(function () {
              document.querySelector('style').style.backgroundColor = colors[rand(colors.length)];
          }, 2000);        
      }
  }
  
  var colors = [
      'red',
      'green',
      'blue',
      'yellow',
      'magenta',
      'pink'
  ];
  
  
  var rand = function (max) {
      return Math.floor(Math.random() * max);
  };</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.style{
      background-color: pink;
      top: 50px;
      left: 50px;
      height: 50px;
      width: 50px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;body&gt;
    &lt;div class="style"&gt;&lt;/div&gt;
    &lt;/body&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>But I can't find out why it doesn't work.<br>
  EDIT: the script also crashes the browser</p>