JavaScript 在 For 循环期间暂停

标签 javascript for-loop settimeout sleep

<分区>

我有以下功能,但未能成功尝试在每次请求 2 秒后添加暂停以不超过 RPM 阈值。

<cfoutput>
<script type="text/javascript" language="JavaScript">

    // Convert ColdFusion variable to JS variable
    #ToScript(Variables.resourceIds, "jsList")#

    // Split list
    var list = jsList.split(",");

    // Loop through list
    for (var i=0; i<list.length; i++) {         
        var pricingSearch = new XMLHttpRequest();
        pricingSearch.open("GET", "doPricing.cfm?ID=" + list[i], false);
        pricingSearch.onload = function (e) { };
        pricingSearch.onerror = function (e) { };
        pricingSearch.send(null);
        console.log('Searching for Id ' + list[i] + '...');
        setTimeout(function() {
            // Wait for a couple seconds
        }, 2000);
        if (i == list.length) {
            console.log('All done!');
        }           
    }
</script>
</cfoutput>

这似乎没有做任何事情,因为循环尽可能快地完成。

我什至尝试过修改代码:

for (var i=0; i<list.length; i++) {         
    setTimeout(function(list,i) {               
        var pricingSearch = new XMLHttpRequest();
        pricingSearch.open("GET", "doPricing.cfm?ID=" + list[i], false);
        pricingSearch.onload = function (e) { };
        pricingSearch.onerror = function (e) { };
        pricingSearch.send(null);
        console.log('Searching for Resource Id ' + list[i] + '...');
        if (i == list.length) {
            console.log('All done!');
        }
    }, 2000);       
}

但这也行不通:(

还有其他人知道我可以使用任何其他方法来实现这一目标吗?

谢谢

最佳答案

您可以使用延迟参数,这样您就可以将每个请求延迟 2 秒。之后。但如果请求有限制,您可以尝试在上一个请求完成后调用下一个请求。

var delay = 2000;
for (var i=0; i<list.length; i++) {         
    setTimeout(function(list,i) {               
        var pricingSearch = new XMLHttpRequest();
        pricingSearch.open("GET", "doPricing.cfm?ID=" + list[i], false);
        pricingSearch.onload = function (e) { };
        pricingSearch.onerror = function (e) { };
        pricingSearch.send(null);
        console.log('Searching for Resource Id ' + list[i] + '...');
        if (i == list.length) {
            console.log('All done!');
        }
    }, delay);
    delay += 2000;
}


//alternative
function callRequest(index){
        if (index == list.length) {
            console.log('All done!');
            return;
        }
        var pricingSearch = new XMLHttpRequest();
        pricingSearch.open("GET", "doPricing.cfm?ID=" + list[index], false);
        pricingSearch.onload = function (e) { callRequest(index+1); };
        pricingSearch.onerror = function (e) { console.log('error on index:' + index);callRequest(index+1)};
        pricingSearch.send(null);
        console.log('Searching for Resource Id ' + list[index] + '...');


}
callRequest(0);

关于JavaScript 在 For 循环期间暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25266807/

相关文章:

javascript - 重新绘制旧 donut 并通过单击每种颜色 Chart.js 创建一个新 donut

java - 如何打印增强型 for 循环的前 10 行

javascript - 如何使用 setTimeout 或 setInterval 减慢循环

javascript - AngularJS 动态表单字段添加必需属性

使用 JavaScript 的 JavaScript 可移动 Div

javascript - 无法正确访问 API 的响应

java - java 如何退出for循环?

Jquery 鼠标悬停/移出 for 循环

JavaScript : setTimeout use on an anonymous function expression

javascript - JavaScript 中使用装饰器时箭头函数和函数的区别