GAS正在将后端脚本与页面加载同步



在我的html代码中,我调用一个脚本来编辑谷歌表单,然后加载该表单。我的问题是,在脚本完成更新之前,网页就会加载。

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<script>
google.script.run.changeForm();     
window.top.location.href='https://docs.google.com/forms/d/e/ABCDpQLSdV2e61UFkG-_LcVBRpWe9F1MizNJ-P5JUCUGRlWiFSoImPkA/viewform';
</script>
<body>
</body>
</html>    

我的临时修复方法是设置Timeout((,但这感觉像是一个肮脏的小把戏。

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<script>
google.script.run.changeForm();  
setTimeout(function(){  
window.top.location.href='https://docs.google.com/forms/d/e/ABCDpQLSdV2e61UFkG-_LcVBRpWe9F1MizNJ-P5JUCUGRlWiFSoImPkA/viewform';
}, 2000);
</script>
<body>
</body>
</html>

有没有更好的方法来解决这个问题?

您应该尝试

google.script.run.withSuccessHandler(success).changeForm();
function success(){
window.top.location.href='https://docs.google.com/forms/d/e/ABCDpQLSdV2e61UFkG-_LcVBRpWe9F1MizNJ-P5JUCUGRlWiFSoImPkA/viewform';
}

withSuccessHandler(success)在函数changeForm()完成执行后运行参数中的函数

最新更新