在 2 秒内按下下一次按钮时发出警报

问题描述 投票:0回答:2

我是 JavaScript 新手。如果有人连续按下按钮,我想在客户端显示一条消息。因此,我认为如果有人在不到两秒的时间内按了两次按钮,最好使用 JavaScript 代码来显示警报消息。我可以怎样做呢?我知道只显示警报消息是这样的:

HTML

<div id="content">
  <button onclick="myVar = setTimeout(myFunction, 2000)">Click Me</button>
</div>

JAVASCRIPT

function myFunction() {
  alert('This is an Alert Message!');
}
javascript html asp.net
2个回答
6
投票

试试这个

var t = 0;
function myFunction() {
  if(t == 1) {
    alert('This is an Alert Message!');
  }
  t=1;
  setTimeout(function(){
    t=0;
  },2000)
}
<div id="content">
     <button onclick="myFunction()">Try it</button>
 </div>


0
投票
var t = 0;
function myFunction() {
  if(t == 1) {
    alert('This is an Alert Message!');
  }
  t=1;
  setTimeout(function(){
    t=0;
  },6000)
}
<div id="content">
     <button onclick="myFunction()">Try it</button>
 </div>
© www.soinside.com 2019 - 2024. All rights reserved.