双击有多快?

发布于 2024-12-22 14:57:09 字数 714 浏览 3 评论 0原文

我有以下 JS 代码

$('#dialog').dialog({
  ...
  buttons: {
    OK: function() {
      callAjax({"some":"param"});
      $('#dialog').dialog('close');
    }
  }
});

var ajaxBatch = [];

function callAjax(cmd) {
  ajaxBatch.push(cmd);
  if (ajaxBatch.length == 1)
    setTimeout(ajaxItself, 1);
}

function ajaxItself() {
  ajaxBatch.push({"minor":"data"});
  $.post('url', { msg: JSON.stringify(ajaxBatch) });
  ajaxBatch = [];
}

理论上我应该总是得到 [{"some":"param"},{"minor":"data"}]

但我经常收到关于 [{"some":"param"},{"some":"param"},{"minor":"data"}] 错误请求的错误日志条目。

怎么会发生这样的事情呢?带 2x 按钮的 A4 鼠标在这里有意义吗?

顺便说一句,我无法重现这种情况,我只是在日志中看到它。用户代理也不限于特定的浏览器。

I have the following JS code

$('#dialog').dialog({
  ...
  buttons: {
    OK: function() {
      callAjax({"some":"param"});
      $('#dialog').dialog('close');
    }
  }
});

var ajaxBatch = [];

function callAjax(cmd) {
  ajaxBatch.push(cmd);
  if (ajaxBatch.length == 1)
    setTimeout(ajaxItself, 1);
}

function ajaxItself() {
  ajaxBatch.push({"minor":"data"});
  $.post('url', { msg: JSON.stringify(ajaxBatch) });
  ajaxBatch = [];
}

Theoretically I should always got [{"some":"param"},{"minor":"data"}].

But frequently I got error log entries on wrong requests with [{"some":"param"},{"some":"param"},{"minor":"data"}] there.

How such things may happen? Do the A4 mice with their 2x button make sense here, or what?

BTW, I can't reproduce the situation, I just see it in logs. And User-Agent is not bounded to particular browser there either.

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

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

发布评论

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

评论(1

巾帼英雄 2024-12-29 14:57:09

浏览器可能已将超过 1 个单击事件排队,因此无论按钮是否可见以供他们继续单击,事件都已启动。

也许在您的 OK: 函数中设置一个标志

if (!dialogIsClosed) {
      callAjax({"some":"param"});
      $('#dialog').dialog('close');
      dialogIsClosed = true;
}

我想浏览器会选择处理事件队列中的项目而不是处理 setTimeout 队列中的项目。

The browser might have queued up more than 1 click event, so whether or not the button is visible for them to keep clicking, the events were already set in motion.

Maybe set a flag in your OK: function

if (!dialogIsClosed) {
      callAjax({"some":"param"});
      $('#dialog').dialog('close');
      dialogIsClosed = true;
}

I would imagine a browser would choose processing an item from the event queue over processing an item from the setTimeout queue.

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