[英]How to say restart the loop in Javascript
我找不到我需要做的事情,所以我正在尋求您的幫助。 我實際上正在做一個獲取所有Docker機器的循環,但是有時出於晦澀的原因,機器是“未定義的”,並且在片刻之后它們正在運行或停止。
我想做的是在我的代碼中放一個東西(我在這里放了//),它停止了所有循環並重新啟動它。
getInfosMachines = Meteor.bindEnvironment(function(){
//console.log("listener running");
//clean the 2 arrays
changeSpotted = false;
stateLastCheckMachine.length = 0;
stateDockerLastCheckMachine.length = 0;
InfosMachines._ensureIndex({ "idMachine": 1});
//add the collection value in the array
InfosMachines.find().forEach(Meteor.bindEnvironment(function(mch){
stateLastCheckMachine.push(mch.stateMachine);
}));
//take the actual values
Machine.list({ inspect: true },Meteor.bindEnvironment(function (err, machines){
machines.forEach(Meteor.bindEnvironment(function(machineInfo){
if(typeof machines === "undefined"){
sleep(2000);
//here
}else{
stateDockerLastCheckMachine.push(machineInfo.state);
}
}));
}));
(是的,不是所有的代碼,但是在這里我需要一些幫助)謝謝您的幫助
[編輯]感謝所有幫助我的人,我從大家中學到了東西!
控制循環的最簡單方法是使用基本的for循環並處理索引。
//Pseudo machines so we have something to work with var machines = [10, 1, 4, 5, 24, 5, 8, 2]; for (var i = 0; i < machines.length; i++) { //Get machine var machine = machines[i]; //Do something with it console.log("iteration", i, "looking at machine", machine); //Restart loop if parameter if (Math.random() > 0.8) { console.log("Restart"); i = 0 - 1; } }
具有內置睡眠的版本,直到所有機器都加載完畢:
var machines = [10, , 4, , 24, 5, 8, 2]; //Wrapped in function so we can easily recall it function testMachines() { //initial loop to test for undefined machines for (var i = 0; i < machines.length; i++) { if (typeof machines[i] == "undefined") { console.log("machine at index", i, "is undefined"); setTimeout(testMachines, 1000); return false; } } console.log("All ready, running loop"); //Pseudo machines so we have something to work with for (var i = 0; i < machines.length; i++) { //Get machine var machine = machines[i]; //Do something with it console.log("iteration", i, "looking at machine", machine); } } //Loading the machines setTimeout(function () { machines[1] = 99; setTimeout(function () { machines[3] = 98; }, 2000); }, 2000); //Start testing testMachines();
您可以創建一個函數
function checkMachines(machines,stateDockerLastCheckMachine){
var stop=false;
machines.forEach(Meteor.bindEnvironment(function(machineInfo){
if(stop){
return;
}
if(typeof machines === "undefined"){
sleep(2000);
checkMachines(machines,stateDockerLastCheckMachine);
stop=true;
}else{
stateDockerLastCheckMachine.push(machineInfo.state);
})
}
您可以“等待”使用以下功能定義所有計算機:
function waitForDefinedMachines() {
return new Promise(function(resolve) {
var machines = ...; // Get machines.
if (typeof machines === "undefined") {
setTimeout(function() {
resolve(waitForDefinedMachines());
}, 1000); // Sleep for 1 second.
} else {
resolve();
}
});
}
如您所見,它返回Promise 。
然后這樣稱呼它:
waitForDefinedMachines().then(function() {
// Do your forEach loop here.
});
(由於我沒有使用Meteor的經驗,所以我從代碼中刪除了它們。顯然,您必須將其重新添加。)
您可以在未定義機器時簡單地減少索引。 它將檢查這台機器,直到它被定義
for(let i=0;i<machines.length;i++){
if(machines[i] === undefined){
//Machine not started
//Decrease index thus this machine will be check again next iteration
i--;
}else{
//Machine has started. Do what you need with it
stateDockerLastCheckMachine.push(machineInfo.state);
}
}
如果要等待2秒鍾再檢查同一台機器,則可以添加等待功能
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
for(let i=0;i<machines.length;i++){
if(machines[i] === undefined){
wait(5000);
i--;
}else{
stateDockerLastCheckMachine.push(machineInfo.state);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.