title: javascript alternatives to setInterval
body: <p>I know that would be a duplicate, but I can't find what I need. I try to make a real time mmorpg (refferences:travian,tribal-wars,ikariam,etc) in order to get some experience and still have some fun (my childhood dream).
  User have more then one city and he can access each one using a '<strong>select form</strong>'.
  When user changes the <strong>select form,</strong> an ajax is going to DB and returns '<strong>current city resources: wood,iron,stone</strong>' also the '<strong>current production</strong>' for each one. All works well. When I change the select form, ajax is updating <strong>resources bar</strong> with a loop over <strong>values class</strong>. I will update my DB stocks table with the current values + productions each hour(would be a waste to make it every 10 seconds). The problem is that I want to run a script every 5-10 seconds which should update client side <strong>resources stock</strong> something like this: "document.getElemByID(wood).html = current dbStocked <strong>wood</strong>(//which has been query once with ajax) + (wood_production/3600(//seconds in a minute)*((current_minutes*60)+(current_seconds)))". All works fine, but when I change the city with select form, setInterval keep running, now having 2 values of <strong>wood</strong> and <strong>wood_prod</strong> in script logic, now on each itineration toogle between this 2 cases. Every 5 seconds the div representing wood value gets:one time last selected city calculation, one time current city calculation. So the div content is juggling each 5 second(interval time). EDIT: The setInterval is kidnaping the value of which one started and not gonna drop it away even replaced by another, so it forces initial values and toogling them with the current ones, every 5 seconds.
  Here is a part of my code:</p>
  
  <pre><code>$(document).ready(
    function() {
      $("#setCitty").on("change", getstats);
    }
  );
  
  
  function getstats() {
    var city = $('#setCitty').val(); //the select form triggering the ajax
    var identifier = $('#identifier').val(); //don t mind it
    $.ajax({
        type: 'post',
        url: 'handle/php/cityStatsGet.php',
        dataType: "json",
        data: {
          city: city,
          identifier: identifier,
        },
        success: function(response) {
          console.log(response.citystats);
          console.log(response.production); //Added console log here...all seems ok, changing city is changing the content of  response.citystats and response.production ..
          clearInterval(interval);
          var v = 0;
          $(".values").each(function() { //i have a mess here, i will use vanilla for performanece later
            $(this).html(response.citystats[v]);
            v++;
          });
          incoming();
          setInterval(incoming, 5000);
  
          function incoming() {
            var d = new Date();
            var m = d.getMinutes();
            var s = d.getSeconds(); //response.citystats[19] is iron stock
            $('#ironInc').html(parseInt(parseInt(response.citystats[19]) + ((parseInt(response.production[2]) / 3600)) * ((+parseInt(m) * 60) + parseInt(s))));
          } //i parseint all because i have a headpain trying to figure out what is wrong just because js treats pure numbers as strings
        });
  
    }
   &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;span class="values" id="ironInc"&gt;&lt;/span&gt;
  </code></pre>
  
  <p>So...my question is...if I can in some way restart the execution of setInterval (because is not restarting when I recall its patern function). 
  EDIT: also, I have in php</p>
  
  <pre><code>$stmt = $conn-&gt;prepare("SELECT * FROM city_stats WHERE user = ? AND city = ? ");
  $stmt-&gt;bind_param('ss', $username, $city);  
  $stmt-&gt;execute();   
  $result = $stmt-&gt;get_result();
  $row = $result-&gt;fetch_assoc();
  //here are some math to get final values eg.:$income = $row['foo']+$row['bar'];
  $data = array();
  
  $data['citystats'] = array(
  $population,$workers,$houses,$happiness,$popularity,$filantropy,$terror,$tax,
  $foodFactor,$innFactor,$religion,$crime,$crowding,$rats,$food,$meat,$fruits,
  $chease,$bread,$kiron,$kstone,$kgold,$kwood
       ); //23 ELEMENTs I&lt;23
  $data['production'] = array(
      $goldincome,$pwood,$piron,$pstone
  );
  $stmt-&gt;free_result();
  $stmt-&gt;close();
  $conn-&gt;close();
          echo json_encode($data);
  </code></pre>