简体   繁体   English

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

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

I am trying to use google.script.run, the asynchronous client-side JavaScript API provided by Google Scripts within a while loop. 我正在尝试使用google.script.run,它是while循环内Google脚本提供的异步客户端JavaScript API I understand that calls to google.script.run are asynchronous so I even tried using a global variable 'window.i' for loop iterations. 我知道对google.script.run的调用是异步的,因此我什至尝试使用全局变量“ window.i”进行循环迭代。

Unfortunitely window.i++ never happens and the browser freezes when I run this code. 不幸的是,当我运行此代码时,window.i ++永远不会发生,并且浏览器会冻结。

Here's the code snippet that I am struggling with: 这是我苦苦挣扎的代码片段:

var iterations = 5;
window.i = 0;
while (window.i < iterations) {
    google.script.run
    .withSuccessHandler(function(){
        window.i++;
     })
     .sendNow();
}

Is there any way I can increment the loop variable on success callback? 有什么方法可以在成功回调中增加循环变量?

Use the success handler to send the next request. 使用成功处理程序发送下一个请求。 Dont use a while loop. 不要使用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