不可控递归如何避免多线程

 代码如下, 自行消化

let time = null;
let requestIndex = 0;
// 延时
let start = (now_) => {
  if(now_ == requestIndex){
    time = setTimeout(() => {
      request(now_);
    }, 5000);
  }
};
// 停止 settimeout
let stop = () => {
  clearTimeout(time);
  time = null;
};

// 請求
let request = (now_) => {
  setTimeout(() => {
    console.log(now_ + '個請求');
    start(now_);
  }, 2000);
};

// 每点击一次需要刷新线程
let push = () => {
  requestIndex += 1;
  request(requestIndex);
};

你可能感兴趣的:(JavaScript,工具类,javascript)