繁体   English   中英

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

[英]Increment while loop on success of asynchronous google.script.run

我正在尝试使用google.script.run,它是while循环内Google脚本提供的异步客户端JavaScript API 我知道对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();
}

有什么方法可以在成功回调中增加循环变量?

使用成功处理程序发送下一个请求。 不要使用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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM