вот простой:
// this is an async timeout util (very useful indeed)
const timeout = async ms => new Promise(res => setTimeout(res, ms));
let next = false; // this is to be changed on user input
async function waitUserInput() {
while (next === false) await timeout(50); // pause script but avoid browser to freeze ;)
next = false; // reset var
console.log('user input detected');
}
Вот пример приложения с jQuery:
async function myFunc() {
// do stuff
await waitUserInput();
$('#text').append('* user has clicked<br>')
await waitUserInput();
$('#text').append('* user has clicked second time')
// next bit
}
$('#user-input').click(() => next = true)
myFunc() // launch function and start waiting for user input
Пожалуйста, посмотрите этот рабочий ДЕМО
// this is an async timeout util (very useful indeed)
const timeout = async ms => new Promise(res => setTimeout(res, ms));
let next = false; // this is to be changed on user input
async function waitUserInput() {
while (next === false) await timeout(50); // pause script but avoid browser to freeze ;)
next = false; // reset var
console.log('user input detected');
}
async function myFunc() {
// do stuff
await waitUserInput();
$('#text').append('* user has clicked<br>')
await waitUserInput();
$('#text').append('* user has clicked second time')
// next bit
}
$('#user-input').click(() => next = true)
myFunc()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id='user-input' style='padding:15px;color:white; background: tomato; border: 0; border-radius:8px; font-weight: bold'>CLICK ME !</button>
<div id='text'>
</div>