Javascript jquery在循环中延迟和承诺

Javascript jquery在循环中延迟和承诺,javascript,jquery,Javascript,Jquery,这里有点不对劲。所有函数都应该同步调用。谁能给我一个提示吗?我认为这是for循环中的一个错误 这是我的密码: var radioValues = msg.options; var cn = gotoReport() .then(clickReport) .then(clickReportFake) .then(clickNext); for (var i = 0; i < radioValues.length; i++){ cn = cn.t

这里有点不对劲。所有函数都应该同步调用。谁能给我一个提示吗?我认为这是for循环中的一个错误 这是我的密码:

  var radioValues = msg.options;
  var cn = gotoReport()
   .then(clickReport)
   .then(clickReportFake)
   .then(clickNext);

   for (var i = 0; i < radioValues.length; i++){
       cn = cn.then(clickOption(radioValues[i])).then(clickNext);
   }
   cn.then(clickSendToFacebook).then(clickFinish);  


//all called functions look like that 
  function clickNext(){
        return process(function(){
            console.log("clickNext");
            var next = $('button:contains("Weiter")');
             $(next).click();
        },3000);
    }   

function process(action, ms) {
      var deferred = $.Deferred();

      timer = setInterval(function() {
        deferred.notify();
      }, 1000);

      setTimeout(function() {
         clearInterval(timer);
         action();
         deferred.resolve();
      }, ms);

//    return deferred;    
      return deferred.promise();
    }


function sleep(ms)
{
    return(new Promise(function(resolve, reject) {        
        setTimeout(function() { resolve(); }, ms);        
    }));    
}

一个主要问题是:

cn.then(clickOption(radioValues[i]))
您没有将
clickOption
函数作为参数传递给
然后调用它。相反,你应该:

cn.then(clickOption.bind(null, radioValues[i]))

请阅读。关键短语:“搜索、研究”和“解释……任何阻碍你自己解决的困难”。另外,请看一看如何创建。如果要同步调用,我不确定为什么要使用
setTimeout
Deferred
s或
Promise
s,因为它们都用于异步代码。我使用setTimeout函数在函数调用之间进行休眠。这不是完整的代码。函数丢失,您在哪里调用
sleep()
?哦,很抱歉,这里的sleep函数错误。此函数可以忽略。;-)“所有函数都应该同步调用。”:你为什么这么认为?它们不是,因为
setTimeout
回调是异步调用的。
cn.then(clickOption.bind(null, radioValues[i]))