Faizy
Faizy

Reputation: 297

Asynchronus functions

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"')

Upvotes: 4

Views: 61

Answers (3)

Suresh Atta
Suresh Atta

Reputation: 122026

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

Upvotes: 2

user6775131
user6775131

Reputation: 31

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

Upvotes: 0

Drummad
Drummad

Reputation: 722

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

Upvotes: 1

Related Questions