title: Adding simple delay to JavaScript code without using external libraries
body: <p>A quick question, tell me please how to add delay to this code.</p>
  
  <p>I guess it is super simple, but I am new to JavaScript.
  I think the answer is somewhere in the beginning within the duration variable.</p>
  
  <p>Here is JavaScript code: </p>
  
  <pre><code>var modern = requestAnimationFrame, duration = 400, initial, aim;
  
  window.smoothScroll = function(target) {
  var header = document.querySelectorAll('.aconmineli');
      aim = -header[0].clientHeight;
      initial = Date.now();
  
    var scrollContainer = document.getElementById(target);
    target = document.getElementById(target);
  
      do {
      scrollContainer = scrollContainer.parentNode;
      if (!scrollContainer) return;
      scrollContainer.scrollTop += 1;
      }
      while (scrollContainer.scrollTop == 0);
  
      do {
      if (target == scrollContainer) break;
      aim += target.offsetTop;
      }
      while (target = target.offsetParent);
  
      scroll = function(c, a, b, i) {
  
          if (modern) {
          var present = Date.now(),
          elapsed = present-initial,
          progress = Math.min(elapsed/duration, 1);
          c.scrollTop = a + (b - a) * progress;
          if (progress &lt; 1) requestAnimationFrame(function() {
          scroll(c, a, b, i);
          });
          }
          else {
          i++; if (i &gt; 30) return;
          c.scrollTop = a + (b - a) / 30 * i;
          setTimeout(function() {scroll(c, a, b, i)}, 20);
          }
      }
  
      scroll(scrollContainer, scrollContainer.scrollTop, aim, 0);
  }
  </code></pre>
  
  <p>By the way it is a great pure JavaScript only code for scrolling on clicking.</p>