简体   繁体   English

在js中的while循环中使用回调时如何停止无限循环

[英]how to stop infinite loop when using callback inside while-loop in js

so i'm creating a game like Connect 4 which ask an input from a user but the problem i'm facing is that i used callback(readline.question) function inside a while loop whenever i start the code it start infinite loop without asking a question from a user.所以我正在创建一个像 Connect 4 这样的游戏,它要求用户输入但我面临的问题是我在 while 循环中使用了 callback(readline.question) function 每当我启动代码时它就开始无限循环而不询问一个用户的问题。 how i can pause it for a while until user answer?我怎样才能暂停一段时间直到用户回答? I've to solve this without using async/await .我必须在不使用async/await的情况下解决这个问题。

 function fetchColumn(player, callback) {
   io.question(`Player ${player}, which Column? `, line => {
        console.log(`You requested "${line}"`);
        chosen_column = line;
        callback(); 
    });
}
let connect4 = new Connect4();
connect4.makeBoard(numRows, numCols, winLength);
while (game_over == 0) {
    connect4.printBoard();
    fetchColumn(current_player,()=>{
    
        console.log(`you entered ${chosen_column}`);
        
        if (chosen_column != 'Q' && chosen_column != 'q') {
            move_status = connect4.place_piece(chosen_column, current_player);
            x_in_a_row_status = connect4.x_in_a_row(current_player);
            
            if (move_status == 0) {
// Further code-------

This is what i'm getting in terminal.这就是我在终端中得到的。

Player 1, which Column? 
A B C D E F G 
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . 
Player 1, which Column? 
A B C D E F G
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

----------Keep repeating----------

If you want to call an asynchronous function in a loop, you can either use while in combination with await :如果要在循环中调用异步 function,可以将whileawait结合使用:

function fetchColumnAsync(player) {
  return new Promise(function(resolve, reject) {
    io.question(`Player ${player}, which Column? `, line => {
      console.log(`You requested "${line}"`);
      chosen_column = line;
      resolve(); 
    });
  });
}
let connect4 = new Connect4();
connect4.makeBoard(numRows, numCols, winLength);
while (game_over == 0) {
  connect4.printBoard();
  await fetchColumnAsync(current_player);
  console.log(`you entered ${chosen_column}`);
  ...
}

or recursion in combination with a callback function:或递归结合回调 function:

function loop() {
  if (game_over == 0) {
    connect4.printBoard();
    fetchColumn(current_player, () => {
      console.log(`you entered ${chosen_column}`);
      ...
      loop();
    });
  }
}
let connect4 = new Connect4();
connect4.makeBoard(numRows, numCols, winLength);
loop();

But you cannot combine while with a callback, because the second iteration of the while loop starts synchronously , before the callback function can be invoked asynchronously .但是您不能将while与回调结合使用,因为while循环的第二次迭代同步开始,然后才能异步调用回调 function。 In other words, it gives an infinite loop.换句话说,它给出了一个无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM