2013-12-14 3 views
5

"getproduct"버튼을 클릭하면 제품을 사용할 수 없으면 ajax 호출이 제품 또는 404 코드를받습니다.성공할 때까지 ajax 호출을 반복하는 방법

제품이 반환 될 때까지 getMyJson이 5 초마다 다시 호출되기를 원합니다.

아이디어가 있으십니까?

$(function() { 
    $("#getproduct").click(function getMyJson() { 
     $.ajax({ 
      type: "GET", 
      url: "Product/GetProduct", 
      dataType: "json", 
      success: function (data) { 
       //alert("succes"); 
       $("#name").html(data.Name); 
       $("#price").html(data.Price); 
      }, 
      error: function() { 
       //alert("fail"); 
       //callback getMyJson here in 5 seconds 
      } 
     }); 
    }); 
}); 

답변

13

당신은 그것을 완벽하게 작동

$(function() { 
    function getMyJson() { 
     $.ajax({ 
      type: "GET", 
      url: "Product/GetProduct", 
      dataType: "json", 
      success: function (data) { 
       //alert("succes"); 
       $("#name").html(data.Name); 
       $("#price").html(data.Price); 
      }, 
      error: function() { 
       //alert("fail"); 
       //callback getMyJson here in 5 seconds 
       setTimeout(function() { 
        getMyJson(); 
       }, 5000) 
      } 
     }); 
    } 
    $("#getproduct").click(function() { 
     getMyJson(); 
    }); 
}); 
+0

error 콜백 함수에 setTimeout을 사용할 수 있습니다. 많은 감사 :-) – Maxime

관련 문제