title: How to add spaces separating thousands in Javascript in this example
body: <p>I have two functions here. One that adds a "," for separating thousands, like 1234 -> 1 234. And one function for increasing.</p>
  
  <p>The function for increasing is just printing 123456 and I would like to combine these, I though I could just change:</p>
  
  <p>$this.html(++current);</p>
  
  <p>to:
  $this.html(addSpaces(++current));</p>
  
  <p>But it's not working. Please help me, how can I fix this?</p>
  
  <pre><code>function addSpaces(nStr)
  {
      nStr += "";
      x = nStr.split(".");
      x1 = x[0];
      x2 = x.length &gt; 1 ? "." + x[1] : "";
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(x1)) {
          x1 = x1.replace(rgx, "$1" + " " + "$2");
      }
      return x1 + x2;
  }   
  
  
  function count($this) {
  
      var current = parseInt($this.html(), 10);
      current = current + 13 /* This is increment */
  
      $this.html(++current);
      if (current &gt; $this.data("count")) {
          $this.html($this.data("count"));
      } else {
          setTimeout(function() { count($this); }, 100);
      }
  
  }
  </code></pre>