Javascript 如何在multile Jquery Get函数中等待

Javascript 如何在multile Jquery Get函数中等待,javascript,jquery,html,json,get,Javascript,Jquery,Html,Json,Get,在下面的代码中,我循环遍历每个域,并通过jQuery GET函数执行一些操作。循环是同步的。如何使每个循环在执行下一个GET请求之前等待5秒 jsons = ["123.com","234.com","456.com"] jQuery.each(jsons, function(key,ele) { var maindomain = ele; var apis= "https://api.panel.com/search?q="+maindomain; $.

在下面的代码中,我循环遍历每个域,并通过jQuery GET函数执行一些操作。循环是同步的。如何使每个循环在执行下一个GET请求之前等待5秒

 jsons = ["123.com","234.com","456.com"]


    jQuery.each(jsons, function(key,ele) {
    var maindomain = ele; 
    var apis= "https://api.panel.com/search?q="+maindomain;
    $.get(apis, function(source) { 

    if(source == "yes")
    {
    $.get("https://database.com/update.php?domain="+maindomain, function(response) { 
    console.log("successs");
    });
    }

    //SLEEP 5 seconds Here
    });
您可以使用
.queue()
$.when()
setTimeout()
.dequeue()

var jsons=[“123.com”、“234.com”、“456.com”],res=[];
var fn=函数(arg){
return$.Deferred(函数d){
d、 解析([arg,“success”,{}])
}).then(功能(数据){
res.push(数据);返回数据
})
}
$({}).queue(“请求”),jsons.map(函数(请求){
返回函数(下一步){
//在这里做ajax的事情
然后(fn.bind(null,请求))
.然后(函数(){
console.log(res)
//在`5000ms`超时后调用`next`函数
设置超时(下一步,5000)
})
}
})).dequeue(“请求”)

您可以使用
.queue()
$.when()
设置超时()
.dequeue()

var jsons=[“123.com”、“234.com”、“456.com”],res=[];
var fn=函数(arg){
return$.Deferred(函数d){
d、 解析([arg,“success”,{}])
}).then(功能(数据){
res.push(数据);返回数据
})
}
$({}).queue(“请求”),jsons.map(函数(请求){
返回函数(下一步){
//在这里做ajax的事情
然后(fn.bind(null,请求))
.然后(函数(){
console.log(res)
//在`5000ms`超时后调用`next`函数
设置超时(下一步,5000)
})
}
})).dequeue(“请求”)


您是否尝试过
setTimeout()
?为什么不使用
closure
的可能副本?您是否尝试过
setTimeout()
?为什么不使用
closure
的可能副本“+maindomain应仅在第一个get请求完成后运行加上一个,您好,我如何才能在此添加错误处理?”snipper@Vishnu您应该能够添加链接到第二个
.fail()
。然后()“+maindomain应仅在第一个get请求完成后运行加上一个,您好,我如何才能在此添加错误处理?”snipper@Vishnu您应该能够添加链接到第二个
.fail()
。然后()
var jsons = ["123.com","234.com","456.com"];

$({}).queue("requests", jsons.map(function(request) {
  return function(next) {
    // do ajax stuff
    $.get("https://api.panel.com/search?q=" + request)
    .then(function(source) {
       if (source === "yes")
       return $.get("https://database.com/update.php?domain=" + maindomain)
    })         
    .then(function() {
      // call `next` function after `5000ms` timeout
      setTimeout(next, 5000)
    })
  }
})).dequeue("requests")