这是一段jquery代码,请帮我修改为,如果五秒内链接不到服务器则打印"未找到服务器": $.post("http://localhost:8080/xxxx/home.do",temp, function(data) { let com = document.getElementById("commodity_row"); let card = ``; $.each(data, function(i, v) { card += ` <div class="col-xl-2 col-lg-3 col-md-3 col-sm-4 col-6 mb-2"> <div class="card card_aive cur-poi card_color"> <img src="http://localhost:8080/xxxx/${v.g_Pic}" class="card-img-top " > <div class="card-body"> <span class="card-text">${v.g_Name}</span> <p class="card-text font-size-12 color-red">${v.gCategories_Id.c_Name}</p> <p id="price" class="card-text text-right color-red">¥${v.g_Money}</p> </div> </div> </div>`; }) com.innerHTML = card; }, "json");
时间: 2024-03-07 19:51:00 浏览: 39
可以使用setTimeout()函数来实现五秒钟超时的效果,然后在超时后打印出"未找到服务器"。修改后的代码如下:
```javascript
$.ajax({
type: "POST",
url: "http://localhost:8080/xxxx/home.do",
data: temp,
dataType: "json",
success: function(data) {
let com = document.getElementById("commodity_row");
let card = ``;
$.each(data, function(i, v) {
card += ` <div class="col-xl-2 col-lg-3 col-md-3 col-sm-4 col-6 mb-2">
<div class="card card_aive cur-poi card_color">
<img src="http://localhost:8080/xxxx/${v.g_Pic}" class="card-img-top " >
<div class="card-body">
<span class="card-text">${v.g_Name}</span>
<p class="card-text font-size-12 color-red">${v.gCategories_Id.c_Name}</p>
<p id="price" class="card-text text-right color-red">¥${v.g_Money}</p>
</div>
</div>
</div>`;
});
com.innerHTML = card;
},
error: function() {
console.log("未找到服务器");
}
});
setTimeout(function() {
console.log("未找到服务器");
}, 5000);
```
其中,$.post()被替换为了$.ajax(),并且加入了error属性来处理请求失败的情况。同时,使用setTimeout()函数来设置五秒钟的超时时间,当超过该时间后仍未收到服务器响应,则打印"未找到服务器"。
阅读全文