title: Calculating mouse velocity with accuracy
body: <p>I made a little script intended to output the speed at which the mouse moves. The issues I am having is that occasionally the script will output an inaccurate number among the numbers and throw the whole thing off. As well, the script does not always terminate when the user releases the mouse button. To solve the first issue I assume I could probably place the values into an array and then determine the outlier, but I'm hoping somebody here can just tell me I'm doing something stupid and there is a way to fix my code to make it more efficient. </p>
  
  <p>JS: </p>
  
  <pre><code>var test_obj = {
      mouse_coors: function(x2) {
  
          $(document).on("mousemove", function(e) {
              var x = e.clientX,
              velocity = Math.abs(x-x2);
  
              console.log(velocity);
              $(document).off("mousemove");
  
              setTimeout(function() {
                  x2 = e.clientX;
                  test_obj.mouse_coors(x2);
              }, 100);
          });
  
          $(document).on("mouseup", function() {
              $(document).off("mouseup").off("mousemove");
          });
      },
  };
  
  
  $(document).ready(function() {
  
      $('#testbox').on("mousedown", function() {
          test_obj.mouse_coors(0);
      });
  
  });
  </code></pre>
  
  <p>HTML:
      
      </p>
  
  <p>JSfiddle: <a href="http://jsfiddle.net/mkb2t/" rel="nofollow">http://jsfiddle.net/mkb2t/</a></p>