0
socket.ioイベントのフロントエンドから渡される変数が異なる同じ関数のインスタンスを複数作成する必要があります。フロントエンドのクリックからその機能を停止できるように、これらの変数を別のsocket.ioイベントにも表示する必要があります。私はすべてがうまく動作していますが、設定変数 'bigLoop'を使用して1つのインスタンスに対してのみ使用でき、複数のインスタンスで動作する必要があります。私は実行することができ、お互いに独立しているためには、すべてのループが必要です。Socket.ioイベントで毎回異なる変数を持つ同じ関数の複数のインスタンスを作成する
ソケットイベント:
//this is what I have so far and it works great, I can start and stop at any time. And I know bigLoop is global
io.on('connection', function(connect) {
//create new loop
connect.on('createLoop', function(data) {
bigLoop = new syncLoop(data.total, function(){}, function(){});
});
//end loop
connect.on('endLoop', function(data) {
bigLoop.break(true);
});
});
//this is what I would like passing an array of multiple variables from the front end and creating a new syncLoop for each variable
//create new loop
connect.on('createLoop', function(data) {
//save array to variable
var newNames = data.array;
//for each variable create new syncLoop
newNames.forEach(function(newLoops) {
newloops = new syncLoop(data.total, function(){}, function() {});
});
});
//end loop
connect.on('endLoop', function(data) {
//save array to variable
var endNames = data.array;
//for each variable end loop
endNames.forEach(function(endLoops){
endLoops.break(true);
});
});
//syncLoop function, for process and exit I have a lot more going on but did not want to write it
function syncLoop(iterations, process, exit){
\t var \t index = 0,
\t done = false,
\t shouldExit = false;
loop = {
next:function(){
if(done){
if(shouldExit && exit){
return exit(); // Exit if we're done
}
}
// If we're not finished
if(index < iterations){
index++; // Increment our index
process(loop); // Run our process, pass in the loop
// Otherwise we're done
} else {
done = true; // Make sure we say we're done
if(exit) exit(); // Call the callback on exit
}
},
iteration:function(){
return index - 1; // Return the loop number we're on
},
break:function(end){
done = true; // End the loop
shouldExit = end; // Passing end as true means we still call the exit callback
}
};
loop.next();
return loop;
};
は、私は別の多くのことを試してみましたが、私はこれを動作させることはできません。私はどんな助けにも感謝します。ありがとうございました。
ありがとうございます!!!!私はちょうどコードを調整し、ループと複数のループを開始、停止しています。 – jonhill13
問題はありません、他人を助けるために私の答えを受け入れてください:) – qxz
私はしましたが、私はまだ15の評判を持っていません。記録されますが、一般公開されません。私は自分の評判を試してみることにして、もう一度やり直す。ありがとうございました。 – jonhill13