第二次按键时的 JavaScript

发布于 2024-08-18 08:46:32 字数 475 浏览 4 评论 0原文

我一直想知道是否有一种简单的方法来检测用户是否在一秒钟内按键盘上的同一字符两次。我已经编写了一些可以工作但不可靠的代码。

var escapeCount = 0;

 function reset() {
   escapeCount = 0;
   setTimeout('reset();', 1000);
 }

 window.onload = function() {
   reset();
 };

 document.onkeyup = function(e) {
   if (!e) var e = window.event;
   var code = e.keyCode ? e.keyCode : e.which;
   if (code == 27) escapeCount +=1;
   if (escapeCount == 2) {
     // stuff on second escape
   }
 };

有更好的方法吗?谢谢

I've been wondering if there was a simple way to detect if a user presses the same character on the keyboard twice within one second. I've written some code that kind of works but it's unreliable.

var escapeCount = 0;

 function reset() {
   escapeCount = 0;
   setTimeout('reset();', 1000);
 }

 window.onload = function() {
   reset();
 };

 document.onkeyup = function(e) {
   if (!e) var e = window.event;
   var code = e.keyCode ? e.keyCode : e.which;
   if (code == 27) escapeCount +=1;
   if (escapeCount == 2) {
     // stuff on second escape
   }
 };

Is there a better way to do this? Thanks

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

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

发布评论

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

评论(2

幻梦 2024-08-25 08:46:33

自按下最后一个字符起 1 秒后重置是有意义的。例子:

var lastChar = -1;

document.onkeyup = function(e) {
  if (!e) var e = window.event;
  var code = e.keyCode ? e.keyCode : e.which;

  if (lastChar == code) {
    // Same key was pressed twice in a row within 1 second.
  } else {
    lastChar = code;
    setTimeout(function() {lastChar = -1;}, 1000);
  }
};

It would make sense to reset after 1 second has passed since the last character was pressed. Example:

var lastChar = -1;

document.onkeyup = function(e) {
  if (!e) var e = window.event;
  var code = e.keyCode ? e.keyCode : e.which;

  if (lastChar == code) {
    // Same key was pressed twice in a row within 1 second.
  } else {
    lastChar = code;
    setTimeout(function() {lastChar = -1;}, 1000);
  }
};
埋情葬爱 2024-08-25 08:46:33

您的计时器每秒都会重置,因此您不仅必须在上次 Escape 后的一秒内再次按 Escape,而且在两次按下之间也必须没有超时。

忘记超时并记住最后一次按键的时间可能更容易:

var lastescapetime= null;

document.onkeyup= function(event) {
    if (event===undefined) event= window.event;
    if (event.keyCode===27) {
        var now= new Date().getTime();
        if (lastescapetime!==null && now<lastescapetime+1000) {
            alert('You double-escaped!');
            lastescapetime= null;
        } else {
            lastescapetime= now;
        }
    } else {
        lastescapetime= null;
    }
};

Your timer resets every second, so you not only have to press Escape again within a second of the last Escape, but that also has to have no timeout in between the presses.

It's probably easier to forget the timeout and just remember the time of the last keypress instead:

var lastescapetime= null;

document.onkeyup= function(event) {
    if (event===undefined) event= window.event;
    if (event.keyCode===27) {
        var now= new Date().getTime();
        if (lastescapetime!==null && now<lastescapetime+1000) {
            alert('You double-escaped!');
            lastescapetime= null;
        } else {
            lastescapetime= now;
        }
    } else {
        lastescapetime= null;
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文