【问题标题】:JavaScript: setTimeOut inside another setTimeOut (nested setTimeOut) to stimulate the API response not workingJavaScript:在另一个 setTimeOut(嵌套 setTimeOut)中的 setTimeOut 以刺激 API 响应不起作用
【发布时间】:2018-10-18 13:02:56
【问题描述】:

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

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

问题: 延迟扫描工作正常,但延迟 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);

【问题讨论】:

  • 您的代码按预期工作,我的意思是您的代码就是代码执行的方式,那么您到底需要什么?因为 delayingAPI 正在按预期执行,它会等待 3 秒来执行,每一个都执行。
  • @JohuderGonzalez:每个 API 调用应该延迟 5 秒触发。相反,所有 3 个 api 调用都会在 5 秒后给出响应。我需要每次通话需要 5 秒,最后总共需要 15 秒。
  • 所以你的代码是立即执行的,你需要将每个索引乘以 api 延迟值
  • @JohuderGonzalez 是的

标签: javascript jquery angular mocking settimeout


【解决方案1】:

您需要将当前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);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
相关资源
最近更新 更多