Home > Article > Web Front-end > How to pop up prompt repeatedly in js
To repeatedly pop up the prompt() method of JavaScript, you need to use a loop or recursion: 1. Loop: use a while loop to continuously pop up the dialog box until the user cancels or enters an empty string; 2. Recursion: use a recursive function, automatically Calls itself until the user cancels or enters an empty string.
How to pop up prompt repeatedly in JavaScript
Use JavaScript's prompt()
method. Repeatedly pops up a dialog box to collect user input. To do this, you need to use loops or recursion.
Method 1: Use a loop
<code class="javascript">while (true) { const input = prompt("请输入内容:"); if (input === null || input === "") { break; } console.log(`用户输入:${input}`); }</code>
This loop will continuously pop up the prompt()
dialog box until the user clicks the cancel button or does not enter anything content.
Method 2: Use recursion
<code class="javascript">function promptRecursive() { const input = prompt("请输入内容:"); if (input === null || input === "") { return; } console.log(`用户输入:${input}`); promptRecursive(); } promptRecursive();</code>
This recursive function will automatically call itself until the user clicks the cancel button or enters nothing.
Note:
The above is the detailed content of How to pop up prompt repeatedly in js. For more information, please follow other related articles on the PHP Chinese website!