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?

Answers:

Answer

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.

Answer

No processor i know can execute statements at the same time. Most computers have multiple processors, so they can run multiple statements on multiple processors. So the only possible solution would be opening your browser twice, open the same page and hope that the js is executed parallel ( or use some fancy NodeJS or WebWorkers etc.).

However instead of running the same time , its common to switch between two threads very fast, so that it looks like being the same time (called multitasking). You could do this in js like this:

var executed = false;
var theother=new Promise( r=>setTimeout(r,0));
func();
// loop 2
func();

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

Or old style ( to resolve the magic ):

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

function func(){
if (executed) return;
setTimeout(function(){
  executed = true;
  alert(1);
 },0);
}
Answer

You can't do this. Because JavaScript does not support multi-threading. Every tab of web browser just have a single interpreter and JavaScript interpreter in the browser is a single thread. See an example of single thread here.

See also this answer.

Answer

Use below code

<!DOCTYPE html>
<html>
<head>
    <title>sample test</title>
    <script type="text/javascript">

        print=function(msg) {
            var ifunction=function(){
                console.log(msg);
            }
            return ifunction;
        };

        button1=function(name,count){
                for (var i =0 ;i <count; i++) {
                    setTimeout(print(name+":"+i+"seconds over after button1 click"),i*500);
                }
        };

    </script>
</head>
<body>
    <form>
        <input type="" name="" />
        <input type="button" value="button1" onclick="button1('button1',5)" />
        <input type="button" value="button2" onclick="button1('button2',5)" />
    </form>
</body>
</html>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.