简体   繁体   English

如何触发延迟的 jQuery 事件?

[英]How to trigger a delayed jQuery event?

I'm writing a workaround for web form.我正在为 web 表单编写解决方法。 The given problem is that hitting Submit pops out a loading animation.给定的问题是点击提交会弹出加载 animation。 The content is saved, but the form is still shown, as is the loading animation.内容已保存,但仍显示表单,加载 animation 也是如此。

The idea is to trigger an event, 2-3 seconds after the Submit click, that will reset the form content and hide the loading animation.这个想法是在提交点击后 2-3 秒触发一个事件,这将重置表单内容并隐藏加载 animation。

How would you suggest to approach this?你会建议如何处理这个问题?

Thank you.谢谢你。

Use the callback functions in the events to chain them together - that way it will make sure that the action has completed, instead of trying to run all of them at the same time.使用事件中的回调函数将它们链接在一起 - 这样它将确保操作已完成,而不是尝试同时运行所有它们。 If you post some code we can probably help some more.如果您发布一些代码,我们可能会提供更多帮助。 There's also the .delay() function, but I think callbacks are more appropriate, because your process is event driven and not time driven.还有.delay() function,但我认为回调更合适,因为您的流程是事件驱动的,而不是时间驱动的。 You have more flexibility in the case that something in the process goes wrong, instead of statically resetting after a click.在过程中出现问题的情况下,您拥有更大的灵活性,而不是在单击后静态重置。

Can you just use a setTimeout(...) that calls a method that resets form content and hides the animation?您可以只使用 setTimeout(...) 来调用重置表单内容并隐藏 animation 的方法吗? See: http://www.w3schools.com/js/js_timing.asp见: http://www.w3schools.com/js/js_timing.asp

jQuery Form came to the rescue. jQuery 表格来救援。

$('form#id').ajaxForm(function() {
    // do stuff
});

This will run the callback after the form is submitted.这将在提交表单后运行回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM