title: How to detect when mouse has stopped
body: <p>I have written the following JavaScript code. I am using it to detect when the mouse is moving and when it has stopped. MouseStopped() function is a loop of hundreds of items that will tell me where the mouse has stopped, so I want to call it only when the mouse has stopped.</p>
  
  <pre><code>var CheckMovement;
  var stopLoop = false;
  var n = 0;
  canvas.addEventListener('mousemove', function (evt) {
      CheckMovement = setInterval(function () { HasMouseStopped(evt) }, 250);
  }, false)
  
  function HasMouseStopped(evt) {
      var mousePos = getMousePos(canvas, evt);
      newMouseX = mousePos.x;
      newMouseY = mousePos.y;
          if ((newMouseX !== mouseX) &amp;&amp; (newMouseY !== mouseY)) {
  
              stopLoop = true;
          } else {
              //stopped moving
              clearInterval(CheckMovement);
              stopLoop = false;
              n = 0;
              MouseStopped();
          }
          mouseX = newMouseX;
          mouseY = mousePos.y;
  }
  
  function MouseStopped() {
      while (arr.length &gt; n) {
          if (stopLoop) { break; }
          if (ctx.isPointInPath(mouseX, mouseY)) {
              //tooltip text
              ctx.font = '12pt Candara';
              ctx.fillStyle = 'black';
              ctx.fillText(arr[n], mouseX + 10, mouseY - 5);
              break;
          }
          n++;
      }
  }
  </code></pre>
  
  <p>Now I have the following problems:</p>
  
  <ol>
  <li>Even though I am calling <code>clearInterval(CheckMovement)</code>, it doesn't
  stop iterating; it is running continuously, which cause the problem of
  calling <code>MouseStopped()</code> multiple times. <strong>Why is it not stopping?</strong></li>
  <li>I would like to break  <code>MouseStopped()</code> in the middle of its operation if the mouse is moved  before it completed its the loop. This is why I am setting <code>stopLoop = true;</code> However, that also doesn't seem to be working as intended. <strong>How can I achieve these?</strong></li>
  </ol>
  
  <p>Thanks.</p>
  
  <p>EDITS</p>