0

How to apply a lock to 2 functions so that the 1st function gets executed 1st and then the second function executes when called simultaneously.

The 2 functions are :

function f1(){
   //some code here
}

function f2(){
   //some code here
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
user2168945
  • 3
  • 4
  • 10
  • Are they asynchronous? – zerkms Mar 19 '13 at 08:44
  • try using callback process.. – Sudip Pal Mar 19 '13 at 08:44
  • This might be your answer: http://stackoverflow.com/questions/39879/why-doesnt-javascript-support-multithreading –  Mar 19 '13 at 08:45
  • @user2168945 - are you looking for plain javascript or JQuery solution? – Sandeep Kumar M Mar 19 '13 at 08:45
  • yes they are ajax functions. – user2168945 Mar 19 '13 at 08:48
  • @user2168945 - Please provide more details. How are you actually calling the two functions. Some concrete code samples would be of great help. What do you mean that they are called simultaneously? Are they being queued in the event loop using `setTimeout`? Are they suspended and waiting for an external event to occur? Please be more specific when you ask questions. – Aadit M Shah Mar 19 '13 at 15:30

1 Answers1

1

Functions in JavaScript aren't "called simultaneously" : there is only one user thread.

You don't have to put a lock, you have to look at how you call the functions. And probably you don't have to care.

If what you want is having two functions executed in order when the ajax requests are done, then you may use jQuery's promise system :

$.when($.ajax(...), $.ajax(...)).done(function(){
  f1();
  f2();
});
drunken bot
  • 486
  • 3
  • 8
  • U mean only 1 function runs at a time. Because consider that both functions have a for loop in them then if after 3 iterations of 1st function I call 2nd then both functions iterations should be displayed i.e 1st function should continue from 4 and 2nd function should also start simultaneously – user2168945 Mar 19 '13 at 08:50