Realization method of synchronization of different JS asynchronous functions _javascript Skills

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.