简体   繁体   English

Javascript:停止直到按下 RETURN 键

[英]Javascript: Stop until RETURN key is press

I'm newbie with javascript and I'm not able to figure out how to stop and wait until return key has been pressed.我是 javascript 的新手,我无法弄清楚如何停止并等到按下返回键。

Any help is appreciated.任何帮助表示赞赏。

Thanks in advance.提前致谢。

You could either do it with a callback function, or using async await to wait a specific key is pressed:您可以使用回调 function 来执行此操作,也可以使用async await来等待按下特定键:

 const keyIsPressed = target => new Promise(resolve => { document.body.addEventListener('keyup', ({ key }) => { if(key.toUpperCase() === target.toUpperCase()) { resolve(); } }, { once: true }); }); async function start() { console.log('Waiting for user to press enter...'); // pause here until enter is pressed await keyIsPressed('enter'); console.log('Enter is pressed.'); } start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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