Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AJAX API调用之间等待时保持输出顺序_Javascript_Jquery_Ajax_Delay - Fatal编程技术网

Javascript 在AJAX API调用之间等待时保持输出顺序

Javascript 在AJAX API调用之间等待时保持输出顺序,javascript,jquery,ajax,delay,Javascript,Jquery,Ajax,Delay,我有一个jQuery脚本,它调用web API以获得JSON格式的输出。该API设计用于少量连续调用。我需要从API中获取更多的数据,但我不想一个接一个地手动发送输入,因为这需要很长时间 我不在乎脚本运行需要多长时间,只要不需要手动操作。所以我增加了一些延迟。问题是结果现在不符合我发送给API的顺序,因此我无法将输出与输入连接起来 如何保持textareaElem中的行顺序与输入对齐 var time = 0; for (var i = 0; i < value_array.length;

我有一个jQuery脚本,它调用web API以获得JSON格式的输出。该API设计用于少量连续调用。我需要从API中获取更多的数据,但我不想一个接一个地手动发送输入,因为这需要很长时间

我不在乎脚本运行需要多长时间,只要不需要手动操作。所以我增加了一些延迟。问题是结果现在不符合我发送给API的顺序,因此我无法将输出与输入连接起来

如何保持textareaElem中的行顺序与输入对齐

var time = 0;
for (var i = 0; i < value_array.length; i++) {
  if (i > 0 && i % 5 == 0) {
    time = Math.floor((Math.random() * 45) + 15) * 1000; // Random waiting
  } else {
    time = 0;
  }

  (function (textareaElem, value_array, i) {
    window.setTimeout(function() {
      $.getJSON("myscript.php?var="+encodeURIComponent(value_array[i]), function(result) {
        textareaElem.val(textareaElem.val() + (result['val'] + '\n');
      });
    }, time);
  })(textareaElem, value_array, i);
}
var-time=0;
对于(变量i=0;i0&&i%5==0){
time=Math.floor((Math.random()*45)+15)*1000;//随机等待
}否则{
时间=0;
}
(函数(textareaElem、值数组、i){
setTimeout(函数(){
$.getJSON(“myscript.php?var=“+encodeURIComponent(value_array[i]),函数(result){
textareaElem.val(textareaElem.val()+(结果['val']+'\n');
});
},时间);
})(textareaElem,value_数组,i);
}
编辑:我的代码中有一个错误。我需要脚本将API调用暂停一段时间。上面的代码只会在处理其他行时延迟某些行的处理。这是因为setTimeout()是异步的

这也引入了顺序的问题。顺序可以通过@Rodrigo Juarez answer解决,但这对整个问题没有帮助,因为我需要每隔几次调用暂停API调用


如何暂停?

当结果
.length
等于
value\u数组
.length
时,尝试将结果推送到对象中包含
I
的数组中,根据
I
对存储的数组进行排序,然后设置
textraelem
.val()

var time = 0, order = [];
for (var i = 0; i < value_array.length; i++) {
  if (i > 0 && i % 5 == 0) {
    time = Math.floor((Math.random() * 45) + 15) * 1000; // Random waiting
  } else {
    time = 0;
  }

  (function (textareaElem, value_array, i) {
    window.setTimeout(function() {
      $.getJSON("myscript.php?var="+encodeURIComponent(value_array[i])
      , function(result) {
        var curr = {index:i, value: result['val'] + '\n'};
        order.push(curr);
        if (order.length === value_array.length) {
          order.sort(function(a, b) {
             return a.index - b.index
          });
          textareaElem.val(order.join(""))
        }
      });
    }, time);
  })(textareaElem, value_array, i);
}
var-time=0,order=[];
对于(变量i=0;i0&&i%5==0){
time=Math.floor((Math.random()*45)+15)*1000;//随机等待
}否则{
时间=0;
}
(函数(textareaElem、值数组、i){
setTimeout(函数(){
$.getJSON(“myscript.php?var=“+encodeURIComponent(值_数组[i]))
,函数(结果){
var curr={index:i,value:result['val']+'\n'};
订单推送(curr);
if(order.length==value\u array.length){
排序(函数(a,b){
返回a.index-b.index
});
text区域元素值(order.join(“”)
}
});
},时间);
})(textareaElem,value_数组,i);
}

这是我找到的解决方案,只需创建和数组,您已经拥有每个请求的索引,因此您只需将数组中的值分配给每个结果,然后使用join方法

 var time = 0;
var text = [];
var index = 0;

//function to be called to retrieve the data
var getData = function (index) {
      $.getJSON("myscript.php?var="+encodeURIComponent(value_array[index]), function(result) { 
        text[index] = result.val + '\n';
        textareaElem.val(text.join(""));
        if (index < value_array.length){
          getData(index++);
        }
    });
};

//Just calling the function with index 0
if (value_array.length) {
  getData(0);
}
var-time=0;
var text=[];
var指数=0;
//要调用以检索数据的函数
var getData=函数(索引){
$.getJSON(“myscript.php?var=“+encodeURIComponent(value_array[index]),函数(result){
text[index]=result.val+'\n';
textareaElem.val(text.join(“”);
if(索引<值\数组长度){
getData(index++);
}
});
};
//仅调用索引为0的函数
if(值\数组长度){
getData(0);
}

你能试试这段代码吗?我创建了一个字符串数组,我使用for的索引来存储不同的字符串,因此所有字符串数组都将有序这看起来不错,但我不确定我是否有错误。Will window.setTimeout()实际上,保持循环并等待当前延迟的索引完成?由于重新索引的整个问题,看起来setTimeout()保持对当前索引的调用,但循环同时继续。我需要“暂停”循环暂停API调用。你认为你能帮上忙吗?是的,没问题。检查此代码我使用forEach更新了它,而不是传统的forEach循环,forEach为每次迭代创建一个新的作用域,因此它将保留索引。请告诉我提供的答案是否对你有效,并将其标记为正确。顺序已用此修复。Bu这并不能解决整个问题,因为我需要真正暂停API调用。请参阅我问题中的编辑。您可以尝试这样的操作吗?现在,在API结束每个调用后,它将发出下一个请求,如果您想延迟该调用,您只需在setTimeout前后使用您想要的延迟包装对getData()的调用。