title: How to Animate Scroll to Top JavaScript
body: <p>I have got 2 websites with the same function. It works on one of the websites, doesn't work on the other.</p>
  
  <p><code>script.js</code>:</p>
  
  <pre><code>function scrollToTop(){
  var timerHandle = setInterval(function() {
    if (document.body.scrollTop != 0 || document.documentElement.scrollTop != 0)
    window.scrollBy(0,-50); else clearInterval(timerHandle); },10);
  }
  </code></pre>
  
  <p><code>HTML</code></p>
  
  <pre><code>       &lt;ul class="footer-navigation"&gt;
          &lt;li&gt;&lt;a class="back" id="backtoTop" onclick="scrollToTop();return false;"href="#"&gt;&amp;uarr;Back to Top&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a class="home" href="#home"&gt;Home&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a class="about" href="#about"&gt;About&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a class="contact" href="#contact"&gt;Contact&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
  </code></pre>
  
  <p>It doesn't hit any error but it seems as though it is "counting". When I <code>console.log</code> it there's a number in the console next to the logged <code>counting</code> but function never executes.</p>
  
  <p>Thanks </p>