[英]How to repeat ajax call until success
單擊“getproduct”按鈕時,如果產品不可用,則ajax調用將獲取產品或404代碼。
我希望每5秒再次調用函數getMyJson,直到返回產品。
有任何想法嗎 ?
$(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
}
});
});
});
您可以在error
回調函數中使用setTimeout
$(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();
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.