如果onsubmit函数运行时间超过10秒,则HTML表单提交两次

如何解决如果onsubmit函数运行时间超过10秒,则HTML表单提交两次

我为此奋斗了整整一整天,发现的搜索结果都对我没有帮助。

我正在Google表格中编写脚本,并在Windows 10 OS上使用Chrome浏览器。我至少需要在Chrome中运行它,因为我的公司大多数都使用Chrome。

当我的代码完全生效时,一切正常,但是html表单中的onsubmit操作调用了我的TWICE中的函数,我不知道为什么。

我所看到的是;不管我的html文件或gs函数中的内容是什么,如果我的函数执行需要十秒钟或更长时间,那么它将被第二次调用。怎么会这样我在做什么错了?

一步一步,这就是我在做什么和会发生什么。

首先,通过附加菜单,我调用初始函数,该函数会生成包含表单的HTML模型窗口。

function importPool() {
var ui = SpreadsheetApp.getUi();
var html = HtmlService.createHtmlOutputFromFile("importPool")
.setWidth(750)
.setHeight(300);
var dialog = ui.showModalDialog(html,"Import pool");
}

这将启动“ importPool.html”

<!DOCTYPE html>
<html>
<head>
<base target="_top">

</head><body>
<form id="myForm" onsubmit="processForm(myForm);">
    <div style=" text-align: left; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;">
        <table width="100%" border="1" cellpadding="2" cellspacing="2" style="background-color: #c0c0c0;">
    
            
            <tr valign="top">
                <td style="border-width : 0px;text-align: left"></td>
                <td style="border-width : 0px;text-align: right">
                    <input type="button" value="Cancel" onclick="google.script.host.close()"><input type="submit" value="Import">
      </td>
            </tr>
    
        </table>
    </div>
</form>
<script>
function processForm(formdata){
  google.script.run.processImport(formdata);
  google.script.host.close();
}
</script>
</body>

</html>

我单击我的“提交”按钮,这在我的gs中调用了processImport函数。为了测试目的,该功能仅包含

function processImport(form) {
Logger.log("I am running!");
Utilities.sleep(12000); 
}

正如我提到的,如果我减少了计时器,以便该函数执行不到十秒钟,则每次单击“提交”按钮时,该函数仅运行一次。但是,如果函数执行时间超过十秒钟,则在单击“提交”时,它将运行两次。它第一次正常运行,然后在运行时间几乎恰好十秒后再次从上到下执行该功能。

可能是什么原因造成的?我确信我做的事很愚蠢,但我找不到。请帮忙!

解决方法

改进建议

  1. 通过事件侦听器替换内联事件处理程序(onsubmit=onclick=属性)
  2. 使用withSuccessHandler时使用withFailureHandlergoogle.script.run
  3. google.script.host.close()的回调中添加witchSuccessHandler
  4. <script></script>的开头添加以下内容
// Prevent forms from submitting.

  function preventFormSubmit() {
    var forms = document.querySelectorAll('form');
    for (var i = 0; i < forms.length; i++) {
      forms[i].addEventListener('submit',function(event) {
        event.preventDefault();
      });
    }
  }
  window.addEventListener('load',preventFormSubmit);

来自https://developers.google.com/apps-script/guides/html/communication#forms

请注意,加载页面中的所有表单后,默认情况下都会提交 由preventFormSubmit禁用的操作。这样可以防止页面 发生异常时重定向到错误的URL。


来自Introduction to events

您可以找到许多事件处理程序属性的HTML属性等效项。但是,您不应该使用这些-它们被认为是不好的做法。如果您只是在快速地做某事,使用事件处理程序属性似乎很容易,但是它们很快变得难以管理且效率低下。

请记住,google.script.run调用是异步的(它们之后的代码将在服务器端函数结束之前执行)

相关

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-