admin管理员组文章数量:1287555
This is some homework I have. I want it to read user input and then create two objects, giving them values based on the user input. Here is my code, beginning at the top of the JS file:
console.log("Type 'help' for mands");
console.log("Square numbers:");
console.log("Enter player 1 name");
var player1 = new Player(readLine());
var player2 = new Player(readLine());
console.log("logging name after making players" + player1.name);
function readLine() {
var inputText;
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter player name? ", function(answer) {
inputText = answer;
console.log("Player name:", answer);
rl.pause();
});
return inputText;
}
Here is the output I get: .jpg
I'm used to C# and Java and in those languages, readline() would wait for the input and then create the object with whatever I inputted, before moving onto the next line. Not in JS though! It just carries on, calling readline() twice before I've typed anything in (therefore printing "prompt>" twice) and then logging player 1's name (before typing it in, hence undefined). Then it waits for me to type something in and once I type something, it sets both players to the one name and then ends the program.
Is it possible to do what I am trying to get it to do? Is there another way I can get input to work without it skipping past it and running the rest of the code?
Thanks in advance!
This is some homework I have. I want it to read user input and then create two objects, giving them values based on the user input. Here is my code, beginning at the top of the JS file:
console.log("Type 'help' for mands");
console.log("Square numbers:");
console.log("Enter player 1 name");
var player1 = new Player(readLine());
var player2 = new Player(readLine());
console.log("logging name after making players" + player1.name);
function readLine() {
var inputText;
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter player name? ", function(answer) {
inputText = answer;
console.log("Player name:", answer);
rl.pause();
});
return inputText;
}
Here is the output I get: http://oi60.tinypic./9tn7nn.jpg
I'm used to C# and Java and in those languages, readline() would wait for the input and then create the object with whatever I inputted, before moving onto the next line. Not in JS though! It just carries on, calling readline() twice before I've typed anything in (therefore printing "prompt>" twice) and then logging player 1's name (before typing it in, hence undefined). Then it waits for me to type something in and once I type something, it sets both players to the one name and then ends the program.
Is it possible to do what I am trying to get it to do? Is there another way I can get input to work without it skipping past it and running the rest of the code?
Thanks in advance!
Share Improve this question edited Feb 26, 2015 at 17:48 28Ostriches asked Feb 26, 2015 at 4:50 28Ostriches28Ostriches 411 gold badge1 silver badge6 bronze badges 4-
Did you consider
readline
or do you have to write it yourself for the assignment? – loganfsmyth Commented Feb 26, 2015 at 5:01 -
sys
has been deprecated for a very long time, please useutil
! – Brendan Commented Feb 26, 2015 at 5:45 - @loganfsmyth Thanks guys, I've changed it to node's readline but the problem remains, it runs readline() twice and continues with the rest of the code before I've typed anything in. – 28Ostriches Commented Feb 26, 2015 at 14:45
- Could you update your code in the question? Your existing code has a number of problems and it'll be much easier to demonstrate the right approach with Node's readline. – loganfsmyth Commented Feb 26, 2015 at 17:18
1 Answer
Reset to default 8You have made several incorrect assumptions about JavaScript. The primary issue being that rl.question(...)
shows the prompt to the user, but it can do nothing to block the JS thread itself, so your readline()
function will immediately exit with undefined
because function(answer)
has not run yet because the user has not typed anything yet. A simple way to structure your code would be:
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.write("Type 'help' for mands\n");
rl.write("Square numbers:\n");
function createPayer(number, callback){
rl.question("Enter player " + number + " name? ", function(answer) {
var player = new Player(answer);
// Call the callback function once the player is created.
callback(player);
});
}
createPayer(1, function(player1){
createPayer(2, function(player2){
console.log("logging name after making players" + player1.name);
console.log("logging name after making players" + player2.name);
// Then call player logic you have from in here.
});
});
The key being that there is no explicit 'wait' anywhere in here. JS code is primarily event and callback based, so instead of waiting for the user to type something, you queue up a function to be called once they have entered something.
本文标签: How to get JavaScript console user input working (nodejs)Stack Overflow
版权声明:本文标题:How to get JavaScript console user input working (node.js)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741235095a2362854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论