自定义JavaScript的 Sleep 函数

9年前
//sleep函数  var sleep = (function(){      var queue = [],          isFree = true;      return function(fn, delay){          var args = arguments,              self = this;          if(isFree){              isFree = false;              setTimeout(function(){                  fn();                  isFree = true;                  if(queue.length !== 0){                      args.callee.apply(self, queue.shift());                  }              }, delay);          }else{              queue.push(args);          }      }  })();    // 测试  var card = document.getElementById('a_magic_visit');  for(var i = 0; i < 100; i=i+1){   try{    sleep(function(){     card.click()    },2000);     sleep(function(){     ajaxpost('magicuse_form_visit')    },2000);    sleep(function(){     hideMenu()    },2000);   } catch(e){}  }