GAS功能真正结束后如何运行功能? [英] How to run a function after a GAS function has really ended?
问题描述
在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 ofmyFunc()
. google.script.run is run with the asynchronous process. By this, whenfunc()
is run,serverFunc()
,myFunc(e)
andotherFunc()
in order, whilefunction(nama)
ofgoogle.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()
likefunc()
.
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屋!
- In order to avoid this, please modify
- 为避免这种情况,请像