如何在onsumbit表单上等待异步操作

发布于 2024-10-08 11:34:31 字数 135 浏览 8 评论 0原文

我添加了一个钩子来表单提交事件(表单提交上还有其他事件) 并且需要使用 createelement(script) 将提交报告给另一个域上的服务器。

如何使提交函数等待以确保 createelement(script) 已成功访问服务器?

I've added a hook to form submit event (there are other events on the form submit )
and need to report the submit to a server on another domain using createlement(script).

How can I cause the submit function to wait to make sure the createlement(script) has successfully accessed the server ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筱武穆 2024-10-15 11:34:31

像这样的事情怎么样:

<script>
  function handleSubmit(form) {
    doSomethingAsynchron(
       function() { // callback function
          form.submit();
       }
    );
    return false;
  }
</script>

<form onsubmit="return handleSubmit(this);" ... >
...
</form>

How about something like this:

<script>
  function handleSubmit(form) {
    doSomethingAsynchron(
       function() { // callback function
          form.submit();
       }
    );
    return false;
  }
</script>

<form onsubmit="return handleSubmit(this);" ... >
...
</form>
oО清风挽发oО 2024-10-15 11:34:31

尝试使用 JavaScript 事件。您可以注册任何在特定事件发生时要调用的函数。使用一些预定义事件来表示代码完成,或者使用类似的东西雅虎!库来创建自定义事件,它可以代表服务器请求的完成(找到最后一个一个简短的谷歌搜索,但看起来很有希望)。

我期望 JS 有一个内置的自定义事件架构,但显然它没有。因此,您需要使用像 Yahoo! 这样的外部库。事物。也许这种情况在不久的将来会发生变化。这超出了我的知识范围。但我肯定会推荐使用事件,因为它们完全解决了您所描述的问题。

(或者,您可以像 Thariama 所描述的那样进行忙等待。在可能的情况下,通常不鼓励使用睡眠周期或不忙等待。)

插图:

<script>
// register function b() for custom event

function a() {
    //upload
    // (takes time... but no busy wait)

    //fire custom event
}

function b() {
    //do some other code
}
</script>

<body>

<form onsubmit="a()">
</form>

</body>

Try to make use of JavaScript events. You can register any function to be called when a certain event occurs. Either utilise some of the predefined events to signal the completion of your code or use something like some Yahoo! library to create custom events which could represent the completion of the server request (found the last one with a short Google search, but looks promising).

I expected JS to have a built-in custom event architecture but obviously it has not. Thus you need to use external libraries like this Yahoo! thing. Maybe this changes in the near future. This is above my knowledge. But I can surely recommend using events because they solve exactly the problem you described.

(Alternatively you can do busy wait like Thariama described. Either with sleep cycles or not busy waiting in general is discouraged where possible.)

Illustration:

<script>
// register function b() for custom event

function a() {
    //upload
    // (takes time... but no busy wait)

    //fire custom event
}

function b() {
    //do some other code
}
</script>

<body>

<form onsubmit="a()">
</form>

</body>
〃温暖了心ぐ 2024-10-15 11:34:31

一个不太优雅的解决方案是使用时间间隔来检查脚本是否已访问服务器(检查响应)。为此,请使用 setInterval

A not very elegant solution would be to use a time interval to check if the script to has acsessed the server (check the response). For this use setInterval.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文