setTimeout doesn´t stop
<p>I´m trying to make a script with this goals:</p>

<ol>
<li>Check every X seconds if there is a new order ( function refreshIntervalId() )</li>
<li>if there is a new order, stop the setTimeover</li>
<li>perform some actions in another function ( function refreshIntervalId2() ) and start de setTimeover again</li>
</ol>

<p>I achieve that with this code, it works but the timer never stops and it keeps executing every 5 seconds.</p>

<p>Do you know how can I solve this?
Thanks in advance</p>

<pre><code>  var refreshIntervalId, refreshIntervalId2;

  $("input:text:visible:first").focus();

  refreshIntervalId2 = (function() {
    return {
      update: function() {
        var pedidoid, url;
        clearInterval(refreshIntervalId);
        pedidoid = $("#pedidoid").val();
        url = Routing.generate("get_pedido", {
          id: pedidoid
        });
        return $.getJSON(url, function(json) {
          clearInterval(refreshIntervalId2);
          if (json.entregado === 1) {
            return location.reload(true);
          }
        });
      }
    };
  })();

  refreshIntervalId = (function() {
    return {
      update: function() {
        var url;
        url = Routing.generate("get_pedidos");
        return $.getJSON(url, function(json) {
          var imgsrc;
          clearInterval(refreshIntervalId);
          $(".hero-unit").children("h1").text("Pedido nuevo!");
          $(".hero-unit").children("h2").text("");
          imgsrc = "/images/uploads/" + json.pedido.material.foto;
          $("#imagenpedidomaterial").attr("src", imgsrc);
          $(".cajapuesto").css({
            position: "absolute",
            top: json.pedido.puesto.y + "px",
            left: json.pedido.puesto.x + "px"
          });
          $(".cajapuesto").addClass(json.kolorea);
          $("#divimagenmaterial").show("slow");
          $("#divcodbar").show("slow");
          $("#pedidoid").val(json.pedido.id);
          return setInterval(refreshIntervalId2.update, 5000);
        });
      }
    };
  })();

  $(document).ready(function() {
    return setInterval(refreshIntervalId.update, 5000);
  });

  $(document.body).on("keypress", function(e) {
    var $micodbar, url;
    switch (e.which) {
      case 13:
        $("#divmaterialincorrecto").hide();
        $micodbar = $("#checkcodbar").val();
        url = Routing.generate("get_material", {
          codbar: $micodbar
        });
        return $.ajax(url, {
          type: "GET",
          contentType: "application/json",
          success: function(data) {
            var datos;
            if (data === "null") {
              return $("#divmaterialincorrecto").show("slow");
            } else {
              datos = jQuery.parseJSON(data);
              return $(".row").show("slow");
            }
          },
          error: function(xhr, ajaxOptions, thrownError) {
            console.log("Errorea");
            alert(xhr.status);
            return alert(thrownError);
          }
        });
    }
  });
</code></pre>
