简体   繁体   English

防止双击事件 Javascript

[英]Prevent double on.click event Javascript

i'm still new in using java script, i try to make trigger time when first input field get click, then the function trigger will active, the problem every time i click input field, the triggger always active again, i try using我仍然是使用 java 脚本的新手,我尝试在第一个输入字段被点击时设置触发时间,然后 function 触发器将激活,每次我点击输入字段时都会出现问题,触发器总是再次激活,我尝试使用

event.preventDefault();

but it's didn't work, please help me where i do it wrong?但它没有用,请帮助我哪里做错了? this my code这是我的代码

 $('#trigger').on("click", function(e) { e.preventDefault(); var timeleft = 1; var downloadTimer = setInterval(function(){ document.getElementById('countdown').value = `${timeleft} seconds`; timeleft += 1; }, 1000); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="trigger" class="form-control" required> <input type="text" id="countdown" class="form-control" required>

edit: i try it like this, but its still haunt me if is this the best method?编辑:我这样尝试,但如果这是最好的方法,它仍然困扰着我吗?

 function myFunction() { var timeleft = 1; var downloadTimer = setInterval(function() { document.getElementById('countdown').value = `${timeleft} seconds`; timeleft += 1; }, 1000); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="trigger" onclick="myFunction(); this.onclick=null;" class=" form-control" required> <input type="text" id="countdown" class="form-control" required>

You can use focusout event of Textbox您可以使用 Textbox 的 focusout 事件

$( "#trigger" )
  .focusout(function() {
   myFunction();
  });

and after focusout you can disable Textbox在focusout之后你可以禁用文本框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM