2013-01-16 44 views
0

我正嘗試使腳本與此目標:的setTimeout doesn't停止

  1. 檢查每X秒,如果有一個新的訂單(功能refreshIntervalId())
  2. 如果有一個新的秩序,制止setTimeover
  3. 在另一個函數執行一些操作(功能refreshIntervalId2()),並開始去setTimeover再次

我實現與此代碼,它的工作原理,但定時器從未停止,並保持EXEC每5秒鐘一次。

你知道我該如何解決這個問題嗎? 在此先感謝

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); 
      } 
     }); 
    } 
    }); 

回答

4

這是因爲refreshIntervalIdrefreshIntervalId2,不會對計時器的引用,而是將包含一個方法update我估計對象的引用命名空間的目的。

clearInterval需要一個定時器作爲參數,這些可以從setInterval調用的返回值中獲取。

您必須滿足這種形式的代碼,以引用您的計時器爲未來的清算:

//myTimer is a reference to your timer created by setInterval. 
var myTimer = setInterval(func,interval); 

//to clear myTimer, pass it to clearInterval 
clearInterval(myTimer); 

事情是這樣的結構就足夠了:

$(document).ready(function() { 
    var firstInterval 
    , secondInterval 
    ; 

    //if you are not creating local scopes, I suggest ditching the IFFE 
    //and go for a literal object instead. 
    var refreshIntervalId = { 
    update : function(){ 
     ... 
     return $.getJSON(url, function(json) { 
     ... 
     clearInterval(firstInterval); 
     ... 
     }); 
     ... 
    } 
    }; 

    //run refreshIntervalId.update every 5 seconds 
    //timer referenced by firstInterval 
    firstInterval = setInterval(refreshIntervalId.update, 5000); 
}); 

只是順便說一句,你的keypress處理程序應該在$(document).ready()中,以確保DOM元素在操作之前被加載。

+0

Ey!謝謝!我做了改變,它的工作,但我現在有另一個問題,json變量是空的,它不會等待,直到它執行getJSON調用,它一直在代碼內部http://pastebin.com/4nf7xbb9 – ikerib

+0

@ ikerib這可能是由於AJAX的異步性質。這可能對另一個問題有好處。 – Joseph

+0

問題出在後端。謝謝大家! – ikerib