JavaScript:另一个 setTimeOut(嵌套的 setTimeOut)中的 setTimeOut 刺激 API 响应不起作用

JavaScript: setTimeOut inside another setTimeOut (nested setTimeOut) to stimulate the API response not working

要求:用户连续扫描文本框中的作业编号,没有任何延迟。对于每个作业编号,我需要在后台调用 API 以获取扫描作业编号的详细信息。

我做了什么: 我写了一个小的模拟代码来刺激这个需求。 我正在延迟用户扫描并延迟 API 响应以及 setTimeOuts

问题: 延迟扫描工作正常,但延迟 API 响应无效。

jsbinlink code in jsbin

请运行下面的代码并检查控制台。

var dataset = [10, 20, 30];
var delay = 100;
var apiDelay = 3000;



function execute(dataset) {    
    var i = 0;
    scannedJob(dataset, i);
}


//1ST TIME OUT METHOD//
function scannedJob(dataset, i) {

    setTimeout(function() {
              console.log("Scanned Job Number ="+dataset[i]+" @  time = " + new Date().toLocaleTimeString());


        fireJobSearchHttpAPI1(dataset[i]);

        i++;

        if (dataset.length > i) {
            self.scannedJob(dataset, i);
        } else {
            i = 0;
        }
    }, delay);
}

//2nd TIME OUT METHOD//

/* TWO TYPES OF METHODS I WRITTEN , BOTH ARE NOT WORKING,
EXPECTED:  Each API call should fire with 5sec delay. Instead all 3 api calls given response after 5 seconds. I need each call should take 5sec and total at the end it should take 15 seconds., 
BUT FIRING ALL THE JOB NUMBERS IMMEDIATELY WITHOUT DELAY
*/
function fireJobSearchHttpAPI1(jobNum) {
        setTimeout(function() {
            console.log("job number '"+jobNum+"' API FIRED @ " + new Date().toLocaleTimeString());
        }, apiDelay);

}


function fireJobSearchHttpAPI2(jobNum) {
    (function myLoop(i) {
        setTimeout(function() {
            console.log("job number '"+jobNum+"' API FIRED  @ " + new Date().toLocaleTimeString());
           if (--i) myLoop(i);
        }, apiDelay)
    })(1);

}


//Main Method
execute(dataset);

您需要将当前 index 作为参数传递,因此您需要将该索引乘以当前 api 调用延迟,所以第一步是什么时候(索引= 0), 所以你应该做类似 (index + 1) * apiDelay 这样的事情然后它将是 5000,第二步,索引将是 1 然后 (index + 1) * apiDelay 将是 10000,最后一步 index 将是2,那么(index + 1) * apiDelay就是15000.

在底部对您当前的代码进行了一些修改。

var dataset = [10, 20, 30];
var delay = 100;
var apiDelay = 5000;



function execute(dataset) {    
    var i = 0;
    scannedJob(dataset, i);
}


//1ST TIME OUT METHOD//
function scannedJob(dataset, i) {

    setTimeout(function() {
              console.log("Scanned Job Number ="+dataset[i]+" @  time = " + new Date().toLocaleTimeString());


        fireJobSearchHttpAPI1(dataset[i], i);

        i++;

        if (dataset.length > i) {
            self.scannedJob(dataset, i);
        } else {
            i = 0;
        }
    }, delay);
}

//2nd TIME OUT METHOD//

/* TWO TYPES OF METHODS I WRITTEN , BOTH ARE NOT WORKING,
EXPECTED:  Each API call should fire with 5sec delay. Instead all 3 api calls given response after 5 seconds. I need each call should take 5sec and total at the end it should take 15 seconds., 
BUT FIRING ALL THE JOB NUMBERS IMMEDIATELY WITHOUT DELAY
*/
function fireJobSearchHttpAPI1(jobNum, index) {
        console.log("index", ((index + 1) * apiDelay))
        setTimeout(function() {
            console.log("job number '"+jobNum+"' API FIRED @ " + new Date().toLocaleTimeString());
        }, ((index + 1) * apiDelay));

}


function fireJobSearchHttpAPI2(jobNum) {
    (function myLoop(i) {
        setTimeout(function() {
            console.log("job number '"+jobNum+"' API FIRED  @ " + new Date().toLocaleTimeString());
           if (--i) myLoop(i);
        }, apiDelay)
    })(1);

}


//Main Method
execute(dataset);