4

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?

12
  • 3
    Javascript is like most programming languages, everything is executed in order, unless you explicitly use something that runs asynchronously or uses threads (e.g. Javascript WebWorkers). Commented Jun 9, 2017 at 19:58
  • 1
    When you write func(); func();, it doesn't execute the second call until the first call returns. Commented Jun 9, 2017 at 19:59
  • @Barmar unless they are async Commented Jun 9, 2017 at 20:00
  • 2
    JavaScript has no (standard) provision for concurrent execution contexts (ie. "threads") that can interact. Asynchronicity and concurrency are different. Therefore, "at the same time" should be better qualified. Commented Jun 9, 2017 at 20:00
  • Node JS is the closest thing I know of to true async Commented Jun 9, 2017 at 20:01

4 Answers 4

4

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.

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

Comments

3

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);
}

Comments

0

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.

Comments

-4

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>

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.