title: Run Loop In Background
body: <p>So, I am needing to run an infinite loop in the background of my app (written in JS) that will be used to cycle a ScrollableView every six seconds. However, while this loop runs, I'm unable to preform any other operations in the app as you would think.</p>
  
  <p>To sum up, how can I run this loop at all times while still making the app operational?</p>
  
  <p>Code:</p>
  
  <pre><code>function startScrolling() {
      for(; ; ) {
          sleep(6000);
          Ti.API.info('Scrolling To Index: ' + viewIndex);
          scrollView.scrollToView(viewIndex);
          if(viewIndex == 4) {
              viewIndex = 0;
              scrollView.scrollToView(viewIndex);
          } else {
              scrollView.scrollToView(viewIndex);
              viewIndex++;
          }
      }
  }
  
  function sleep(milliseconds) {
      var start = new Date().getTime();
      while((new Date().getTime() - start) &lt; milliseconds) {
          // Do nothing
      }
  }
  </code></pre>
  
  <p><strong>EDIT: Solution</strong></p>
  
  <pre><code>setInterval(function() {
      Ti.API.info('Scrolling To Index: ' + viewIndex);
          scrollView.scrollToView(viewIndex);
          if(viewIndex == 4) {
              viewIndex = 0;
              scrollView.scrollToView(viewIndex);
          } else {
              scrollView.scrollToView(viewIndex);
              viewIndex++;
          }
  }, 6000);
  </code></pre>