2013-12-14 9 views
5

जब बटन "getproduct" क्लिक किया जाता है, तो AJAX कॉल उत्पाद या 404 कोड प्राप्त करता है यदि उत्पाद उपलब्ध नहीं है।सफल होने तक AJAX कॉल को दोहराना कैसे करें

मैं चाहता हूं कि फ़ंक्शन प्राप्त हो जाए, जब तक उत्पाद वापस नहीं आ जाता है, तब तक हर 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

आप error कॉलबैक फ़ंक्शन

$(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

यह सही काम करता है में setTimeout उपयोग कर सकते हैं। बहुत धन्यवाद :-) – Maxime

संबंधित मुद्दे