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

标签 javascript jquery angular mocking settimeout

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

我做了什么: 我编写了一个小模拟代码来激发这个需求。 我使用 setTimeOuts 延迟用户扫描并延迟 API 响应

问题: 延迟扫描工作正常,但延迟 API 响应不起作用。

jsbin 链接 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);

最佳答案

您需要将当前索引作为参数传递,因此您需要将该索引乘以当前的API调用延迟,因此第一步是什么时候(索引= 0) ,所以你应该这样做 (index + 1) * apiDelay 那么它将是 5000,第二步,index 将是 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);

关于JavaScript:setTimeOut 位于另一个 setTimeOut 中(嵌套 setTimeOut)以刺激 API 响应不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52874690/

相关文章:

javascript - Android Web View 中的非 native 滚动器性能

javascript - 如何以编程方式检测对象是否为 jQuery 对象?

javascript - Apple 取消了仅在网络应用程序中在 ios 6 上流式传输 shoutcast 和 icecast 的可能性?

javascript - AJAX HTML 标签不会显示为 HTML 标签,而是在验证时显示为普通文本

jquery - 如何使用 jQuery 查找上一个表格单元格?

angular - 将 Angular 5 中的 Http 升级到 HttpClient : "server responded with a status of 415 (Unsupported Media Type)"

javascript - React Router v4(未找到页面)服务器端渲染

javascript - 如何通过单击java脚本中动态创建的按钮来获取动态创建的文本框的值

javascript - Angular 8 : Array . map() .slice() 没有函数

javascript - 更新交叉轴上的 D3 多线图表