Wait For An Event To Occur Within A Function In Javascript For Skulpt
Solution 1:
You need to return a Promise from your inputfun, which you then resolve when return gets pressed. (If you're not familiar with Promises, they are well worth a look - <grossSimplification>they let you write asynchronous code using synchronous logic</grossSimplification>).
Something like this (using your code as a base):
Sk.configure({
inputfun: function () {
// the function returns a promise to give a result back later...returnnewPromise(function(resolve,reject){
$("#input").on("keyup",function(e){
if (e.keyCode == 13)
{
// remove keyup handler from #output
$("#input").off("keyup");
// resolve the promise with the value of the input fieldresolve($("#input").val());
}
})
})
},
output: outf,
read: builtinRead
});
I've assumed an input element with an ID of input is receiving stdin. Looking at your OP, you were talking about a textarea (although JQuery .val() should work fine on this). On the basis of what you've called your textarea ("output") it seems that your intention was for both stdin and stdout to involve a shared textarea. If so, I'm not sure how you're going to be able to distinguish between what's been generated by stdin and what's been (subsequently) entered by the user. Thus the code above assumes a separate input field.
I had a similar use case to you, and the way I gave the illusion of an integrated console for both stdin and stdout was to have a span with contenteditable at the end of my output div. On keycode == 13, before I resolved the promise and sent back what the user had typed, I took the text out of the input span and appended it to the output div.
Post a Comment for "Wait For An Event To Occur Within A Function In Javascript For Skulpt"