gpt4 book ai didi

JavaScript 在 For 循环期间暂停

转载 作者:行者123 更新时间:2023-11-30 17:20:12 25 4
gpt4 key购买 nike

<分区>

我有以下功能,但未能成功尝试在每次请求 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);
}

但这也行不通:(

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

谢谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com