Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 异步google.script.run成功时增量while循环_Javascript_Asynchronous_Google Apps Script - Fatal编程技术网

Javascript 异步google.script.run成功时增量while循环

Javascript 异步google.script.run成功时增量while循环,javascript,asynchronous,google-apps-script,Javascript,Asynchronous,Google Apps Script,我试图在一段时间内使用循环。我知道对google.script.run的调用是异步的,所以我甚至尝试使用全局变量“window.I”进行循环迭代 不幸的是,window.i++从未出现过,当我运行此代码时,浏览器冻结 下面是我正在处理的代码片段: var iterations = 5; window.i = 0; while (window.i < iterations) { google.script.run .withSuccessHandler(function(){

我试图在一段时间内使用循环。我知道对google.script.run的调用是异步的,所以我甚至尝试使用全局变量“window.I”进行循环迭代

不幸的是,window.i++从未出现过,当我运行此代码时,浏览器冻结

下面是我正在处理的代码片段:

var iterations = 5;
window.i = 0;
while (window.i < iterations) {
    google.script.run
    .withSuccessHandler(function(){
        window.i++;
     })
     .sendNow();
}
var迭代次数=5;
窗口i=0;
while(window.i<迭代次数){
google.script.run
.withSuccessHandler(函数(){
window.i++;
})
.sendNow();
}

是否有任何方法可以在成功回调时增加循环变量?

使用成功处理程序发送下一个请求。不要使用while循环

//these dont have to be global
var i = 0;
var iterations = 5;
function send() {
  google.script.run
  .withSuccessHandler(function() {
    i++;
    if (i < iterations) {
      send();
    }
  })
  .sendNow();
}
//这些不必是全局的
var i=0;
var迭代次数=5次;
函数send(){
google.script.run
.withSuccessHandler(函数(){
i++;
if(i<迭代次数){
send();
}
})
.sendNow();
}

使用成功处理程序发送下一个请求。不要使用while循环

//these dont have to be global
var i = 0;
var iterations = 5;
function send() {
  google.script.run
  .withSuccessHandler(function() {
    i++;
    if (i < iterations) {
      send();
    }
  })
  .sendNow();
}
//这些不必是全局的
var i=0;
var迭代次数=5次;
函数send(){
google.script.run
.withSuccessHandler(函数(){
i++;
if(i<迭代次数){
send();
}
})
.sendNow();
}

为什么要使用异步成功处理程序?为什么不在while循环的外部和内部增加它呢?因为while循环不会等待异步成功回调。它将继续执行循环,最终您的浏览器将挂起up@kumkanillam这将一次触发所有请求。sendNow()的顺序性很重要;在进程中,只有第一次运行后的剩余记录。为什么要使用异步成功处理程序?为什么不在while循环的外部和内部增加它呢?因为while循环不会等待异步成功回调。它将继续执行循环,最终您的浏览器将挂起up@kumkanillam这将一次触发所有请求。sendNow()的顺序性很重要;由于在处理中,仅在第一次运行后保留记录。谢谢!我目前的配额不足,因此无法测试。我会在一天结束前用这种方法做一些测试,然后再给你回复。我用
setTimeout
测试了它,谢谢!我目前的配额不足,因此无法测试。我会在一天结束前用这种方法做一些测试,然后回复您。我用
setTimeout