A method to prevent a button from being clicked many times in a short time

  • 2020-03-30 02:21:06
  • OfStack

If a button can be clicked many times within a short period of time, it may be maliciously clicked by the user. To prevent this situation, it can be set that the button can only be clicked once within a certain period of time and cannot be clicked at other times.

The code is as follows:
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>Js Timing of events </title> 
<script src="js/jquery-1.9.1.js" type="text/javascript"></script> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<input type="button" value=" My next point " id="btn" onclick="show()" /> 
</div> 
<script type="text/javascript"> 
 

var nn = 30; 
var tipId; 
function show() { 
tipId = window.setInterval("start()", 1000); //The start() method is called every 1 second
} 

function start() { 
if (nn > 0) { 
var vv = " My next point (" + nn + ")"; 
$("#btn").attr("disabled", "disabled"); //Make the button unclickable
$("#btn").attr("value", vv); //Change the text on the button
nn--; 
} else { 
nn = 30; 
$("#btn").removeAttr("disabled"); //Enable the button to be clicked
$("#btn").attr("value", " My next point "); //Change the text on the button
window.clearInterval(tipId); //Clear loop event
} 
} 
</script> 
</form> 
</body> 
</html> 

Related articles: