Nodejs setInterval并立即运行,而不是在interval之后



我尝试在我的noejs应用程序中实现一个循环,它将始终在任务之间等待。为此,我找到了setInterval函数,我认为这是我的解决方案。但我发现,第一个间隔意味着第一个动作也要等到间隔准备好。但我希望第一个动作立即运行,然后每个动作都有给定的间隔。在任意范围内:当myArray[1..][10]将以间隔等待时间开始。

我试过了:

function rollDice(profilearray, browserarray, url) {
return new Promise((resolve, reject) => {
var i = 0;
const intervalId = setInterval(
(function exampleFunction() {
console.log(profilearray[i].profil);
//########################################################################
createTestCafe("localhost", 1337, 1338, void 0, true)
.then((tc) => {
testcafe = tc;
runner = testcafe.createRunner();
inputStore.metaUrl = url;
inputStore.metaLogin = teamdataarray[0].email;
inputStore.metaPassword = teamdataarray[0].password;
inputStore.moderator = profilearray[i].profil;
inputStore.message = profilearray[i].template;
inputStore.channelid = profilearray[i].channelid;
})
.then(() => {
return runner
.src([__basedir + "/tests/temp.js"])
.browsers(browserarray)
.screenshots("", false)
.run()
.then((failedCount) => {
testcafe.close();
if (failedCount > 0) {
console.log(profilearray[i].profil);
console.log("No Success. Fails: " + failedCount);
//clearInterval(intervalId);
//reject("Error");
} else {
console.log(profilearray[i].profil);
console.log("All success");
//clearInterval(intervalId);
//resolve("Fertig");
}
});
})
.catch((error) => {
testcafe.close();
console.log(profilearray[i].profil);
console.log("Testcafe Error" + error);
//clearInterval(intervalId);
//reject("Error");
});
//######################################################################
i++;
console.log("Counter " + i);
if (i === profilearray.length) {
clearInterval(intervalId);
resolve("Fertig");
}
return exampleFunction;
})(),
3000
); //15 * 60 * 1000 max time to wait (user input)
});
}

我所做的工作方式不好,因为在第一个动作中它不会启动testcafe。但在所有其他的行动中,它都会这样做。有人知道更好的方法吗?

范围:给出一个数据数组,并对每个数组使用给定的等待时间启动testcafe。3秒到15分钟。因为在某些情况下,15分钟是很长的时间,我想开始第一个没有任何等待时间。我愿意接受任何建议

对于现代JavaScript,应该使用awaitasync而不是thencatch

这将使许多事情变得更容易,并且代码变得更可读。例如,你可以使用常规的for循环来迭代数组,同时在其中执行异步任务。并以与同步代码相同的方式使用try-catch块。

// a helperfunction that creates a Promise that resolves after
// x milliseconds
function wait(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
async function rollDice(profilearray, browserarray, url) {
for (let i = 0; i < profilearray.length; i++) { 
// depending how you want to handle the wait you would create
// the "wait"-Promise here
// let timer = wait(3000)
let testcafe = await createTestCafe("localhost", 1337, 1338, void 0, true);

try {
let runner = testcafe.createRunner();
inputStore.metaUrl = url;
inputStore.metaLogin = teamdataarray[0].email;
inputStore.metaPassword = teamdataarray[0].password;
inputStore.moderator = profilearray[i].profil;
inputStore.message = profilearray[i].template;
inputStore.channelid = profilearray[i].channelid;
let failedCount = await runner.src([__basedir + "/tests/temp.js"])
.browsers(browserarray)
.screenshots("", false)
.run()
if (failedCount > 0) {
// ...
} else {
// ...
}
} catch (err) {
console.log(profilearray[i].profil);
console.log("Testcafe Error" + error);
} finally {
testcafe.close();
}
// Here you would wait for the "wait"-Promise to resolve:
// await timer;
// This would have similar behavior to an interval.
// Or you wait here for a certain amount of time.
// The difference is whether you want that the time the 
// runner requires to run counts to the waiting time or not.
await wait(3000)
}
return "Fertig"
}

setInterval之前声明函数,运行setInterval(exampleFunction, time),然后正常运行函数(exampleFunction())。可能不是理想的毫秒,但如果你不需要完全精确,应该工作得很好。

询问是否需要进一步帮助

编辑:现在看两次,为什么你在setInterval内部调用你作为参数传递给setInterval的函数?

相关内容

最新更新