Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can javascript run multiple functions at once?

Is it possible to call multiple functions at the same time?

E.g.

var executed = false;
// loop 1
func();
// loop 2
func();

function func(){
    if (executed) return;
    executed = true;
    alert(1);
}

Can func() be executed 2 times at once?

like image 733
user234 Avatar asked Oct 17 '25 17:10

user234


1 Answers

JavaScript has no native support for running multiple functions simultaneously. It has, historically, depended on time-consuming tasks (such as waiting for an HTTP request or doing something CPU intensive) being handled with native code that presents an API to JS (and that API accepting a callback or, more recently, returning a Promise).

That is starting to change.

Most web browsers support Web Workers and Node.js has introduced experimental support for Worker Threads.

These each allow JavaScript code to be farmed off to a separate process which runs independently of the main event loop and communicate with the main process using messages.

like image 191
Quentin Avatar answered Oct 19 '25 06:10

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!