如何让程序等待 JavaScript 中的按钮按下?
我需要修改一些遗留的 JavaScript 代码。有一个地方我想等到用户按下两个按钮之一,然后继续程序流程(如 prompt()
函数)。如何才能实现这一目标?
I need to modify some legacy javascript code. There's a place where I want to wait until the user presses one of two buttons and then continue with the program flow (like prompt()
function). How can this be achieved ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需创建一个承诺并将解析函数存储在其范围之外的全局变量中。然后让程序等待承诺解决(使用“async”和“await”)。通过调用该全局变量,单击按钮即可解决该承诺。我在国际象棋程序中使用了这种技术来等待升级弹出窗口的答案:
因此在创建承诺后,程序将等待直到弹出窗口解决它。弹出窗口在关闭时按如下方式解析承诺:
注意:请记住,在这种情况下,promote_piece() 函数本身也必须使用“await”关键字来调用!否则程序将异步执行并无论如何继续
Simply create a promise and store the resolve function outside its scope in a global variable. Then let the program wait for the promise to resolve (using "async" and "await"). The promise is resolved on a button click by calling that global variable. I used this technique in a chess program to wait for the answer to a promotion popup window:
So after creating the promise, the program will wait until the popup window resolves it. The popup window resolves the promise as follows when it is closed:
N.B. Please keep in mind that in this case the promote_piece() function itself also has to be called with the "await" keyword! Otherwise the program will execute it asynchronously and continue anyhow
您需要在此时中断您的函数,并添加另一个函数来捕获用户的按钮按下事件。
您可以使用 Narrative Javascript 引入阻塞行为并简化它(这样您就不需要破坏将你的函数分成两部分),但请注意,该库自 2013 年以来已被废弃。
You need to break your function at that point, and add another function to catch the user's button press event.
You can use Narrative Javascript to introduce blocking behavior and simplify it (so you don't need to break up your function into two part), but note that this library has been abandoned since 2013.
好吧,也许你想要这种东西,你可以实现事件来标记 Pentium10 答案中的关键:
你可以创建一个名为 waitForIt() 的函数,在其中设置一个 setTimeout() 函数,该函数调用相同的方法,直到全局变量为 true(通过按下按钮操作设置)。
例如:
您可以直接调用 waitForIt() 方法,但我这样做是为了让您可以查看正在发生的情况。
抱歉示例中的混乱,但我没有太多空闲时间:)
Ok, probably you wanted this kind of thing, you can implement the events to flag the key from Pentium10's answer:
You can make a function called for example waitForIt() in which you set a setTimeout() function that calls the same method until a global variable is true (set by you press button action).
For example:
You could call the waitForIt() method directly but i made it so you can view what is happening.
Sorry for the mess in the example but i don't have much time to spare :)
JavaScript 中没有“睡眠”或“等待”运算符。不过,您可以设置一个计时器,当计时器到期时,它将执行一个函数。
您必须使用事件来标记密钥:
There is no "sleep" or "wait" operator in JavaScript. You can set a timer, though, and when the timer expires, it will execute a function.
You have to use the events to flag the key:
我不会为此设置暂停和间歇。
你最好把程序分成两部分,可能就像你建议的两个函数一样。
运行第一部分,添加代码以询问用户操作。
然后根据操作,运行第二部分。
I wouldn't play with timeouts and intervals for that.
You'd be better off cutting the program in 2 parts, may be as you suggest in 2 functions.
Run the first part, add the code to ask the user for action.
And then based on the action, run the second part.