function simulation of different functions to achieve synchronization
Funclist is the queue of function execution functions in which the flag=true is the number of synchronized tokens in the callback function
<script>
var flag = false;
function Functest (t,func) {
settimeout (function () {
(param) {
console.log (param);
Func ();
} (t));
},t*1000);
var funclist = [];
Funclist.push (function () {functest (4,function () {
flag = true;//Synchronous Token
})});//Different asynchronous functions are added to the queue
Funclist.push (function () {functest (3,function () {
flag = true;
})}; /different asynchronous functions are added to the queue
Funclist.push (function () {functest (2,function () {
flag = true;
})}); /different asynchronous functions are added into the queue
Dealfuncsync (funclist);
function Dealfuncsync (funclist) {
function Callbacksync () {
if (!funclist| | funclist.length==0) {
console.log (' End ');
return;
}
Flag = false;
Funclist.shift () ();
settimeout (function () {
if (flag) {//Control queue function Synchronization
callbacksync ();
} else{
settimeout (arguments.callee,100);
}
},100);
}
Callbacksync ();
}
</script>
The above different JS asynchronous function synchronization Implementation method is small series to share all the content, hope to give you a reference, also hope that we support cloud habitat community.