在 jQuery 中测试事件是否在一段时间后发生
我正在为 form-faces xforms 产品编写一个脚本,该产品关闭了内置于 form faces 的事件。该事件称为“xforms-ready”。我将“startTime”定义为文档“就绪”后立即发生。我希望脚本做的是警告用户在“xforms-ready”发生之前花费的时间太长,比如说自“startTime”以来是否已经过了 6 秒。当“xforms-ready”事件发生时,我可以使用下面的代码轻松执行操作:
new EventListener(document.documentElement,
"xforms-ready",
"default",
function() {
var endTime = (new Date()).getTime();
}
);
但是警告将希望在定义“endTime”之前发生。所以我想我想要像这样工作的东西:
If 6 seconds has passed since startTime and endTime is not yet defined do X
或者可能更有效:
If 6 seconds has passed since startTime and 'xforms-ready' has not yet happened do X
任何人都可以建议一种方法吗?
I'm writing a script for a form-faces xforms product that is keyed off an event built into form faces. The event is called 'xforms-ready'. I have define 'startTime' as happening as soon as the document in 'ready'. What I want the script to do is warn the user that it is taking too long before the 'xforms-ready' happens, say if it's been 6 seconds since 'startTime'. I can easily do things when the 'xforms-ready' event happens using the code below:
new EventListener(document.documentElement,
"xforms-ready",
"default",
function() {
var endTime = (new Date()).getTime();
}
);
however the warning will want to happen before 'endTime' is defined. So I guess I want something that works like this:
If 6 seconds has passed since startTime and endTime is not yet defined do X
or possibly more efficiently:
If 6 seconds has passed since startTime and 'xforms-ready' has not yet happened do X
Can anyone suggest a way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
setTimeout
来完成此操作。 (下面是完整的示例。)在 jQuery 的ready
处理程序中,通过setTimeout
设置要在六秒内调用的函数,并在 xforms 就绪处理程序中通过取消该函数如果还没有发生,则清除Timeout。
编辑完整示例(而不是我之前的碎片代码片段),假设您的 xforms 就绪处理程序位于您的 jQuery
ready
处理程序中:(您可能会考虑制作这些命名函数而不是匿名函数,但我为了简单起见,上面使用了匿名。)
You can do this with
setTimeout
. (Complete example below.) In jQuery'sready
handler, set a function to be called in six seconds viasetTimeout
, and in your xforms ready handler, cancel that viaclearTimeout
if it hasn't happened yet.Edit Complete example (rather than my earlier fragmented code snippets), assumes it's okay if your xforms ready handler is within your jQuery
ready
handler:(You might consider making those named functions rather than anonymous ones, but I've used anonymous ones above for simplicity.)