I am working on a project in CodeHS using Javascript. I am trying to collect user input a number of times through a loop. However, I want this input to be asynchronous because readInt is a blocking function and will prevent the following code from being executed until the input popup is finished.
Because of this, I have tried to use the readIntAsync function, which allegedly allows the user to input values without the appearance of the popup box and without the blocking of the following code. However, when I do this, the computer does not appear to store the user input.
For example, in the following code (not from my program), the circle never moves.
var x = 50;
var y = 50;
var circle = new Circle(50);
circle.setPosition(x, y);
add(circle);
async function start() {
for (var i = 0; i < 4; i++) {
var begin = await readIntAsync("Pick a number. ");
println(begin);
x += begin * 50;
y += begin * 50;
circle.setPosition(x, y);
add(circle);
}
}
start();
<script src="https://cdn.codehs.com/chs-js-lib/0.2.21/dist/chs.iife.js"></script>
Is there a way to store the input from an asynchronous input function (readLineAsync, readIntAsync, readBooleanAsync, readFloatAsync). Alternatively, is there another way to get non-blocking input that will be stored?
