单击"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
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
Sat*_*pal 18
您可以setTimeout在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();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7710 次 |
| 最近记录: |