在 jQuery 中测试事件是否在一段时间后发生

发布于 2024-09-03 08:12:38 字数 684 浏览 10 评论 0原文

我正在为 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 技术交流群。

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

发布评论

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

评论(1

Saygoodbye 2024-09-10 08:12:38

您可以使用 setTimeout 来完成此操作。 (下面是完整的示例。)在 jQuery 的 ready 处理程序中,通过 setTimeout 设置要在六秒内调用的函数,并在 xforms 就绪处理程序中通过 取消该函数如果还没有发生,则清除Timeout。

编辑完整示例(而不是我之前的碎片代码片段),假设您的 xforms 就绪处理程序位于您的 jQuery ready 处理程序中

jQuery.ready(function() {
    var xformsReadyTimer;

    xformsReadyTimer = setTimeout(function() {
        // Too long, show the warning
        xformsReadyTimer = undefined;
        alert("XForms is taking too long!");
    }, 6000);

    new EventListener(document.documentElement,
      "xforms-ready",
      "default",
      function() {
          if (xformsReadyTimer) {
              // Cancel the warning
              clearTimeout(xformsReadyTimer);
              xformsReadyTimer = undefined;
          }
      }
    );
});

:(您可能会考虑制作这些命名函数而不是匿名函数,但我为了简单起见,上面使用了匿名。)

You can do this with setTimeout. (Complete example below.) In jQuery's ready handler, set a function to be called in six seconds via setTimeout, and in your xforms ready handler, cancel that via clearTimeout 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:

jQuery.ready(function() {
    var xformsReadyTimer;

    xformsReadyTimer = setTimeout(function() {
        // Too long, show the warning
        xformsReadyTimer = undefined;
        alert("XForms is taking too long!");
    }, 6000);

    new EventListener(document.documentElement,
      "xforms-ready",
      "default",
      function() {
          if (xformsReadyTimer) {
              // Cancel the warning
              clearTimeout(xformsReadyTimer);
              xformsReadyTimer = undefined;
          }
      }
    );
});

(You might consider making those named functions rather than anonymous ones, but I've used anonymous ones above for simplicity.)

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