如何在jQuery中暂停一个事件? [英] How to pause an event in jQuery?

查看:903
本文介绍了如何在jQuery中暂停一个事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对事件有疑问。

基本上,我想在用户点击链接离开页面时运行jquery方法。在这种方法中,我想在他们离开前检查一些事情,所以我知道我可以打电话:

Basically, I want to run a jquery method when the user clicks a link to leave the page. In this method I want to check a few things before they leave, so I know I can call:

e.preventDefault();

但是如果我希望在阻止事件后事件继续怎么办?

But what if I want the event to continue after I prevent the event?

如何处理此建议?

谢谢!

推荐答案

如果方法是同步的,你可以从事件处理程序中调用它。在处理程序返回之前不会执行默认操作,因此您有足够的时间来决定是否要阻止它。

If the method is synchronous you can just call it from within the event handler. The default action will not be executed until the handler returns, so you have as much time as you want to decide if you want to prevent it or not.

如果方法是异步的事情变得更复杂。由于无法等待该方法,因此您必须无条件地阻止默认操作并有条件地再次触发 - 可能使用 .trigger() .triggerHandler() - 异步方法完成后。

If the method is async things get more complicated. Since it will not be possible to wait on the method you will have to unconditionally prevent the default action and trigger it again conditionally -- probably with .trigger() or .triggerHandler() -- after the async method has completed.

这篇关于如何在jQuery中暂停一个事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆