JavaScript while循环等待增量而不是变成无限循环 [英] Javascript while loop waits for increment instead of becoming an infinite loop

查看:101
本文介绍了JavaScript while循环等待增量而不是变成无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在在我的nodejs socket.io游戏"中,我想循环浏览所有当前在线的用户*,每个用户都有机会成为领导者,然后单击一个按钮,该按钮将使下一个用户依次成为领导者(再次,单击使下一个用户成为领导者的按钮.我的问题是,当我让while循环等待"leader"套接字触发leaderSelection事件来增加while循环时,while循环反而创建了一个无限循环,使我的浏览器崩溃.简而言之,我如何才能使while循环等待(理论上是无限长的时间),直到它递增而不是无限地运行.希望这已经足够清楚了.这是我的代码:

Right now in my nodejs socket.io 'game' I would like to cycle through all users currently online*, each having their opportunity to be a leader and click a button that would sequentially make the next user the leader (to, again, click a button that makes the next user the leader. You get the idea). My issue is that when I make the while loop wait for the 'leader' socket to trigger the leaderSelection event to increment the while loop, the while loop instead creates an infinite loop, crashing my browser. Simply put, how can I make a while loop wait (a theoretically infinite amount of time) until it gets incremented, instead of running infinitely. Hopefully this was clear enough. Here's my code:

while(i < ids.length) { //Go through all ids one by one
        //Select current id and make that person the leader
        socket.broadcast.to(ids[i]).emit('leader', { message: 'You are the leader for this round', options: 'THIS WOULD BE A SELECTION BUTTON FOR THE leaderSelection EVENT LISTENER'});
        //Loop through all the other people that are not the leader and say they are the users for this round
        for(var e = 0; e < ids.length; e++) {
            if(i == e) { //Skip current leader
                console.log('skipped ' + usernames[i]); 
                continue;
            } 
            socket.broadcast.to(ids[e]).emit('user', { message: 'You are a user for this round'});
        }
        //When the leader socket clicks the 'select' button, the while loop will go to the next person to be a leader
        socket.on('leaderSelection', function(data) {
            i++; //Here is the issue, the while loop crashes my browser trying to wait for the increment. 
        });
    }

推荐答案

您不能在while循环中执行此操作,Javascript不能那样工作.一种可能的替代方法:

You can't do this in a while loop, Javascript just doesn't work that way. A possible alternative approach:

// Notify the current leader
function notifyLeader(leader) {
    socket.broadcast.to(leader).emit('leader', { message: 'You are the leader for this round', options: 'THIS WOULD BE A SELECTION BUTTON FOR THE leaderSelection EVENT LISTENER'});
}

// Notify all users except the leader
function notifyUsers(users, leader) {
    users
    .filter(user => user !== leader)
    .forEach(user => {
        socket.broadcast.to(user).emit('user', { message: 'You are a user for this round'});
    });
}

// In a round of play, notify the leaders and users
// When the leaderSelection event comes in, start a new round
// with the next leader
function startRound(ids, leaderIndex) {
    notifyLeader(ids[index]);
    notifyUsers(ids, leaderIndex);
    socket.once('leaderSelection', () => {
        startRound(ids, ++leaderIndex);
    });
}

// Start the first round with leader 0
startRound(ids, 0);

这篇关于JavaScript while循环等待增量而不是变成无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆