jquery - 如何根据响应间隔发送 Ajax 请求
问题描述
如何在内部时间发送ajax请求但也取决于响应?如果响应满足某些条件,则不再发送 ajax。太感谢了!
示例代码
function send_ajax_on_interval(){
$.ajax({
type: "GET",
async: true,
url: ...
data: ...
dataType: "json",
success: function (response) {
status = response['stat_code'];
if (status == 0) {
// stop sending requests, otherwise continuously send in every 5 seconds
},
complete:function(){
},
error: function () {
}
});
}
解决方案
您必须在变量中创建间隔,清除它并重新初始化它;
var ajaxInterval;
ajaxInterval = setInterval(send_ajax_on_interval(),5000);
如果条件为真,清除它
if(status == 0){
clearInterval(ajaxInterval);
}
推荐阅读
- css - 如何对齐粘滞页脚内的按钮
- c - 我怎样才能得到单词的第一个元素并将其转换为大写字母,而其他所有字母都转换为小写字母?
- bash - 仅当机器上存在 bash shell 时才使用/(更改为)
- r - 从具有特定字符数的数据框中选择行
- macros - Kentico - 无法通过节点从父节点继承模板获取 DocumentPageTemplateID
- c# - 错误 - 并非所有代码路径都返回值
- java - 如果只有一个 servlet 更改,则强制上传战争?
- php - 到今天在 Symfony 中获得总计数条目?
- sap - 在 BO SAP Web Intelligence 中使用子报表进行过滤
- c++ - strstr() 有 constexpr 版本吗?