title: Add Timeout / Sleep function inside javascript loop
body: <p>I'm trying to add a sleep / delay function inside a js file, This one:</p>
  <pre><code>var webTest = function()
  {
  
  let regex = /^https?:\/\//;
  let url = $('#list_urls').val().split('\n');
  var xmlhttp = [], i;
  var myObj2 = [], i;
   for(let i = 0; i &lt; url.length; i++)
      {
      (function(i) {
      xmlhttp[i] = new XMLHttpRequest();
      url[i] = url[i].replace(regex, '');
      xmlhttp[i].open(&quot;GET&quot;, &quot;https://website.com/API?key=&lt;MY_API_KEY&gt;&amp;url=&quot;+url[i], false);
      xmlhttp[i].onreadystatechange = function() {
        if (xmlhttp[i].readyState === 4 &amp;&amp; xmlhttp[i].status === 200) {
          myObj2 = JSON.parse(xmlhttp[i].responseText);
          document.getElementById(&quot;demo&quot;+i).innerHTML = myObj2.results[1].categories;
        }
      };
  xmlhttp[i].send();
  })(i);
  console.log(`The Server2: `+ myObj2);
   }
  }
  </code></pre>
  <p>I want this script to pause for 10 second and then again do work and then again pause for 10 second and do like this untill text length is greater thatn <strong>i</strong> in loop! My code works if i run for single time but it doesn't works if i run in loop because the <strong>website</strong> has rate limit in the <strong>api</strong> so that's why i'm trying to add a sleep function.</p>
  <p>So what i've tried is <strong>await sleep();</strong> method and also tried <strong>setTimeout</strong> method but it's not working as expected in sort it doesn't works at all with my code!</p>
  <p>await sleep(); just doesn't works at all and displays msg like
  <strong>Uncaught SyntaxError: await is only valid in async functions and async generators webTestfile.js:27</strong></p>