不同ajax 之间 等待 1秒,如何在ajax请求完成后1秒递归调用函数?

尝试将所有这些链接起来:

$(function(){

function checkMessages(){

$.ajax({

type: 'GET',

url: icwsHandlerUrl,

data: {method: 'getMessages', jSON: true},

dataType: 'json',

cache: false,

timeout: 5000,

success: function(data) {

console.log(data); // for testing only but this should call a handler for the data

},

complete: function(){

$.delay(1000, function(){

checkMessages();

});

}

});

}

$(window).load(function(){

checkMessages();

});

});

进入这个:

var req = $.ajax({

type: 'GET',

url: icwsHandlerUrl,

data: {method: 'getMessages', jSON: true},

dataType: 'json',

cache: false,

timeout: 5000,

success: function(data) {

console.log(data); // for testing only but this should call a handler for the data

},

complete: function(){

$.delay(1000, function(){

checkMessages();

});

}

});

var nextTask = req.then(function(data){something});

nextTask.done(function(data){somethingelse});

上面的代码将等待完成下一个任务所需的一切。试试看。

确保删除$.delay()并将其放入req.then()功能。

编辑:

$(window).load(function{

$.delay(1000, function(){

checkMessages();

});

});

并在此代码之外放置检查消息。