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)
    }
}
4
  • There is no generally available way to do what you're asking. Commented Jul 22, 2015 at 5:31
  • ok..I studied there is web worker will will create thread but I did not ubderstant much Commented Jul 22, 2015 at 5:32
  • What exactly do you want to do? What would you need this for? Commented Jul 22, 2015 at 5:53
  • Javascript didnt support multithreading. simple. msdn.microsoft.com/en-us/hh549259.aspx Commented Jul 25, 2015 at 14:49

1 Answer 1

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

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

5 Comments

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 - There are many, many webWorker tutorials out there. 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.
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.
I understand what you are trying to say ..I just want please use webworks so that I will understand more
@Shruti - then go implement one of the tutorials in that link to learn how they work.

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.