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"')
Faizy
  • 299
  • 2
  • 12

3 Answers3

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).

Salman A
  • 262,204
  • 82
  • 430
  • 521
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
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

Community
  • 1
  • 1
Drummad
  • 722
  • 1
  • 6
  • 23
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