Javascript 节点JS中的延迟循环

Javascript 节点JS中的延迟循环,javascript,node.js,Javascript,Node.js,如何在每次调用之间以1秒的延迟循环下面的makeHttpRequest7次我尝试过设置超时和延迟,但似乎无法找到正确的使用方法 var post_data = that.createIRRC("AAAAAQAAAAEAAAB1Aw=="); that.makeHttpRequest(onError, onSucces, "", post_data, false) 如果要在http请求完成后暂停一秒钟,然后再启动下一个请求,则可以在完成回调中使用setTimeout() 而且,由于现在看起来您想

如何在每次调用之间以1秒的延迟循环下面的
makeHttpRequest
7次我尝试过设置超时和延迟,但似乎无法找到正确的使用方法

var post_data = that.createIRRC("AAAAAQAAAAEAAAB1Aw==");
that.makeHttpRequest(onError, onSucces, "", post_data, false)

如果要在http请求完成后暂停一秒钟,然后再启动下一个请求,则可以在完成回调中使用
setTimeout()

而且,由于现在看起来您想要发送一系列代码,您可以在数组中传入您想要发送的代码序列,这将为每个后续代码调用
that.createIRRC()

function sendSequence(cntr, data, delay) {
    // create post_data for next code to send in the sequence
    let post_data = that.createIRRC(data[cntr]);
    that.makeHttpRequest(onError, function() {
        // if we haven't done this limit times yet, then set a timer and
        // run it again after one second
        if (++cntr < data.length) {
            setTimeout(function() {
                sendSequence(cntr, data, delay);
            }, delay);
        } else {
            onSuccess();
        }
    }, "", post_data,false);
}

// define the codes
// this should probably be done once at a higher level where you can define
// all the codes you might want to send so you can reference them by a meaningful
// name rather than an obscure string
let codes = {
    right: "AAAAAQAAAAEAAAAzAw==",
    down: "AAAAAQAAAAEAAABlAw==",
    select: "AAAAAQAAAAEAAABlAw==",
    options: "AAAAAgAAAJcAAAA2Aw==",
    hdmi2: "AAAAAgAAABoAAABbAw==",
    hdmi3: "AAAAAgAAABoAAABcAw=="
}

// create sequence of codes to be sent
let dataSequence = [
   codes.hdmi2, codes.options,
   codes.down, codes.down, codes.down, codes.down, codes.down, codes.down, codes.down,
   codes.right, 
   codes.down, codes.down, codes.down, codes.down,
   codes.select, codes.hdmi3
 ];
// start the process with initial cnt of 0 and 
// send the sequence of data to be sent and 
// with a one second delay between commands
sendSequence(0, dataSequence, 1000);
函数发送序列(cntr、数据、延迟){
//为序列中要发送的下一个代码创建post_数据
让post_data=that.createIRRC(data[cntr]);
makeHttpRequest(onError,function()){
//如果我们还没有完成这个限制时间,那么设置一个计时器并
//一秒钟后再运行一次
如果(++cntr
@Jason-注意,这个答案每秒都会启动一个新的请求。它不会在前一个请求完成后等待1秒(因此这取决于这是否正是您想要的-如果响应变慢,您可能会在这里一次启动多个请求,并且响应甚至可能出现故障,尽管这不太可能)。如果出现错误,它也不会中止,只会继续运行。请注意,这里的
onError
onSuccess
将被称为multipletimes@arboreal84-不,
onError
只会被调用一次。调用onError后循环停止-这是特意设计的
onSuccess
将被多次调用,因为这似乎是OP想要的,否则OP无法从每次调用中访问响应。我应该将createIRRC行放在何处?-我一直在尝试这个代码和@arboreal84的代码,但都无法正常工作:(-我正在尝试向电视发送一系列代码(Option/Down x 6/Right/Down/Select/HDMI3)以选择特定的功能(没有可用的谨慎代码)。如果我使用多个“that.makeHttpRequest(onError,onSucces)”,post_data,false)行有时有效,其他行则会混淆。以下代码供参考:-案例5是我试图发送上述序列的地方。@Jason-您没有描述您试图用
that.createIRRC()执行的操作
在循环内部。您是否只需执行一次,并在循环的每个循环中使用相同的值?或者您是否尝试在循环的每个循环中向该函数传递不同的参数?您问题中的代码只显示了向该函数传递一个常数,因此我假设您可以在循环之前执行一次,然后引用相同的参数每次都是变量。如果要求不同,请显示多个呼叫以及每次传递的内容。您的问题在这方面不清楚。请向我们展示使用
设置超时的尝试
function sendSequence(cntr, data, delay) {
    // create post_data for next code to send in the sequence
    let post_data = that.createIRRC(data[cntr]);
    that.makeHttpRequest(onError, function() {
        // if we haven't done this limit times yet, then set a timer and
        // run it again after one second
        if (++cntr < data.length) {
            setTimeout(function() {
                sendSequence(cntr, data, delay);
            }, delay);
        } else {
            onSuccess();
        }
    }, "", post_data,false);
}

// define the codes
// this should probably be done once at a higher level where you can define
// all the codes you might want to send so you can reference them by a meaningful
// name rather than an obscure string
let codes = {
    right: "AAAAAQAAAAEAAAAzAw==",
    down: "AAAAAQAAAAEAAABlAw==",
    select: "AAAAAQAAAAEAAABlAw==",
    options: "AAAAAgAAAJcAAAA2Aw==",
    hdmi2: "AAAAAgAAABoAAABbAw==",
    hdmi3: "AAAAAgAAABoAAABcAw=="
}

// create sequence of codes to be sent
let dataSequence = [
   codes.hdmi2, codes.options,
   codes.down, codes.down, codes.down, codes.down, codes.down, codes.down, codes.down,
   codes.right, 
   codes.down, codes.down, codes.down, codes.down,
   codes.select, codes.hdmi3
 ];
// start the process with initial cnt of 0 and 
// send the sequence of data to be sent and 
// with a one second delay between commands
sendSequence(0, dataSequence, 1000);