简体   繁体   中英

Can a synchronous input be done in JavaScript?

I'm interested in the possibility of using JavaScript for an introductory language for students. The missing piece for me is the ability to provide a blocking equivalent to the input() command that is available in other languages.

Using console-only versions of JavaScript, like SpiderMonkey , would be a solution. SpiderMonkey has a readline() command. But I think the students would be more interested in code that ran inside a web browser. It would be nice if students could share with their friends their version of the classic "guess a number between 1 and 10" game.

The JavaScript window.prompt() command would also work, but the fact that it pops up a completely separate window isn't ideal. I'd like an in-document input field that works the same way. Is there such a solution?

prompt() is the only thing that comes to mind as a blocking input method. I don't exactly know why you need to make it blocking, but perhaps you can use the jQuery blocking plugin to achieve the blocking (at least the UI)?

I don't think you will find a great way to do that and I'm not sure you should try. I assume this is a very basic course in programming and you want the students to get the feeling they can accomplish something. In that case I would provide them with html code that contains the input field, button and an onclick handler hooked to a function already.

Students will quickly learn to add input fields and buttons. Just because you and I learned to program in text only environments doesn't mean that todays students should.

You con use an input text and a button. when button is pressed get and process the text and clean the input text for a new usage.

In addition you can consider use http://jsfiddle.net/

In a web browser, you can use prompt . It's very old-fashioned (like alert ) and behaves (almost entirely) synchronously. So:

var str;
str = prompt("Enter some text: ", "");
console.log("You entered '" + str + "'");
str = prompt("Enter some more text: ", "");
console.log("You entered '" + str + "'");

Live example | Live source

Instead, though, I think I'd probably recommend getting the students used to using callbacks, and providing some utility code (or just using some pre-built thing). The sooner they get used to it, the better, and it's not that complicated. For instance, the asynchronous version of the above could readily be:

asynchronousPrompt("Enter some text: ", "", function(text) {
    console.log("You entered '" + text + "'");
    asynchronousPrompt("Enter some more text: ", "", function(text) {
        console.log("You entered '" + text + "'");
    });
});

JavaScript中的同步执行为使用JavaScript执行同步操作提供了不错的编写和示例代码。

Would NodeJs be a solution for you? It supports a REPL afaik. And it also has a STDIO implementation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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