1

I know javascript is single thread language ,and code execute line by line .if User need to call two function simultaneously how it can achieve in javascript ? Actually I know in java there is thread which run two task simultaneously can we achieve this here is my code

https://jsfiddle.net/a021oktk/

I need to call both function simutaneouly ?

oneTask();
secondTask()

function oneTask() {
    for (var i = 0; i < 10; i++) {

        console.log("first loop:" + i)
    }
}

function secondTask() {
    for (var i = 0; i < 10; i++) {

        console.log("second loop:" + i)
    }
}
Tushar
  • 85,780
  • 21
  • 159
  • 179
Shruti
  • 1,554
  • 8
  • 29
  • 66

1 Answers1

1

There is no simultaneous execution in plain Javascript as it is single threaded which means that one thread of Javascript runs at a time.

In a browser, you can do some tasks in a webWorker (a separate thread), but webWorkers are very limited in what they can do (they can't modify the page in any way, for example). You can read about webWorkers here on MDN if they are an appropriate tool for what you are doing (it really depends upon what you are doing and what you're trying to accomplish with the results).

In nodejs, there are ways to spawn child processes which can run simultaneously (even using separate CPUs).

If you're interested in creating a long running process that "shares" the CPU with other tasks and "appears" to be running at the same time as other things, you can do your own time slicing with allows other tasks to be interwoven with your task like what is described here. This is not true simultaneous execution, but may appear to be (it depends upon the specific case).

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • how to use webworks I studied doc but I did not understand much if I will take my this example above example how I will achieve the expected result – Shruti Jul 22 '15 at 05:45
  • @Shruti - There are many, many [webWorker tutorials out there](https://www.google.com/search?q=webworker+example+code). Rather than trying to duplicate all that here, I'd suggest you study some of those and then ask a more specific question about them when you get stuck on something in particular. – jfriend00 Jul 22 '15 at 05:47
  • Whether you can solve your problem with webWorkers depends upon what type of operation you actually want to do in each task. You cannot access global variables, cannot access the DOM, cannot call code in your main app. You can basically just run code on its own. The classic usage is to do some heavy computation and then communicate the result back to the main JS thread for use in the web page. You don't say what the actual operation would be in the webWorker so we can't know whether they would work for you or not. – jfriend00 Jul 22 '15 at 05:49
  • I understand what you are trying to say ..I just want please use webworks so that I will understand more – Shruti Jul 22 '15 at 05:49
  • @Shruti - then go implement one of the tutorials in that link to learn how they work. – jfriend00 Jul 22 '15 at 05:50