GAS功能真正结束后如何运行功能? [英] How to run a function after a GAS function has really ended?

查看:74
本文介绍了GAS功能真正结束后如何运行功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在myFunc真正结束后如何使otherFunc()运行?

How to make this otherFunc() run after myFunc has really ended?

async function func() {
  const res = await (() => new Promise(r => google.script.run.withSuccessHandler((e) => r(myFunc(e))).serverFunc()))();
  console.log(res);
  otherFunc();          //this function still executed before myFunc end, except i use setTimeOut
  console.log("done");
}

这是myFunc()内部的内容

This is what is inside of myFunc()

function myFunc(){
  var tbody = document.getElementById("table-permintaan");
  var thead = tbody.firstElementChild;
  while (tbody.hasChildNodes())  
    tbody.removeChild(tbody.firstChild);
  tbody.appendChild(thead);
  google.script.run.withSuccessHandler(function(nama){
   dataArray.forEach(function(r){
     justCreatingTableRows();
  }).listNamaBarang();
}
}

这是otherFunc()内部的内容

This is what is inside of otherFunc()

function otherFunc(){
   var btns = document.getElementsByTagName('button');
  var mdls = document.getElementsByClassName("modal_detail");
  var cds = document.getElementsByClassName("close_detail");
  for(var i = 0;i<btns.length-1;i++){
     btns[i].addEventListener("click",function(){
         document.getElementById("mdl"+this.id.slice(3)).style.display = "block";
     });
     mdls[i].style.display = "none";
  }
  for(var i=0;i<cds.length;i++){
     cds[i].addEventListener("click",function(){
        for(var j = 0;j<mdls.length;j++){
        console.log(this.id.slice(3) , mdls[j].id);
        if (this.id.slice(3) == mdls[j].id.slice(3)) {
           mdls[j].style.display = "none";
           return;
        }   
     }
     });
  }
}

使用promise不会使otherFunc()在myFunc()之后运行,我仍然需要使用setTimeOut,这对这种情况不利.

Using promise didn't make the otherFunc() run after myFunc(), I still need to use setTimeOut which is not good for this case.

推荐答案

修改脚本后,该修改如何?请认为这只是几个答案之一.

When your script is modified, how about this modification? Please think of this as just one of several answers.

  • 在您的脚本中,google.script.run也用于myFunc()的功能. google.script.run与异步进程一起运行.这样,当运行func()时,serverFunc()myFunc(e)otherFunc()依次运行,而google.script.run.withSuccessHandler(function(nama){ })).listNamaBarang()function(nama)以此顺序运行.这样,您的问题就会出现. TheMaster的评论.
    • 为避免这种情况,请像func()一样修改myFunc().
    • In your script, google.script.run is also used in the function of myFunc(). google.script.run is run with the asynchronous process. By this, when func() is run, serverFunc(), myFunc(e) and otherFunc() in order, while function(nama) of google.script.run.withSuccessHandler(function(nama){ })).listNamaBarang() is run out of this order. By this, the issue in your question occurs. This has already been mentioned by TheMaster's comment.
      • In order to avoid this, please modify myFunc() like func().
      async function myFunc() {
        var tbody = document.getElementById("table-permintaan");
        var thead = tbody.firstElementChild;
        while (tbody.hasChildNodes())  
          tbody.removeChild(tbody.firstChild);
        tbody.appendChild(thead);
      
        // Below script was modified.
        const res = await (() => new Promise(r => google.script.run.withSuccessHandler(function(nama) {
          dataArray.forEach(function(r){justCreatingTableRows(r)});
          return r("done at myFunc()");
        }).listNamaBarang()))();
        console.log(res) // done at myFunc()
      }
      

      • 在这种情况下,我认为不需要修改其他功能.
      • 此修改假设声明了dataArray.
        • In this case, I think that other functions are not required to be modified.
        • This modification supposes that dataArray is declared.
          • google.script.run类
            • Class google.script.run

              google.script.run是HTML服务页面中可用的异步客户端JavaScript API,可以调用服务器端Apps脚本函数.

              google.script.run is an asynchronous client-side JavaScript API available in HTML-service pages that can call server-side Apps Script functions.

            • 这篇关于GAS功能真正结束后如何运行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆