简体   繁体   中英

How to apply lock to 2 functions in javascript

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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM