4

When a window.alert() is called, the script shouldnt wait for the user input(clicking ok for example), it should continue.

How could I achieve that without using the setTimeout() function?

this.alert('Window')
console.log('Continue here, dont wait for user input on the "Window"')
6
  • 1
    alert is blocking.. Use some custom UI to display alert instead of window.alert Commented May 3, 2017 at 10:22
  • you can't block in javascript. Commented May 3, 2017 at 10:22
  • @Faizy the keyword is modal box take a look at that topic. Commented May 3, 2017 at 10:26
  • @DanielA.White, try this console.time("alert"); alert("...waiting"); console.timeEnd("alert"); If you cannot block, the measured duration should be like < 1ms Commented May 3, 2017 at 10:29
  • @Thomas you can't block other than those functions.... Commented May 3, 2017 at 10:29

3 Answers 3

2

No. JavaScript is single threaded. You simply can't. Unless the user input hook is completed, thread pauses there.

You can just tweak/have the DOM look like an alert with styling (weird but ok).

Sign up to request clarification or add additional context in comments.

Comments

1

The alert function will pause any further execution of javascript until the user clicks the OK button because the code all runs in a single thread.

You may find this question useful as it addresses the precise problem you are trying to overcome.

Prevent alert() from halting the execution of JavaScript

Comments

0

Since JavaScript runs on single thread, alert/prompt/confirm will pause the execution until response is received from user.

But there are ways you can prevent this:- 1. Use custom UI instead of browser's. 2. By using WebWorkers API 3. By using setTimeout/setInterval 4. By using requestNextFrame function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.