It has always been thought that in web development, double-click events are rarely used. Until recently, the project needed to bind two events, click and double-click, to a button. At first I thought it was just a matter of tying two events to the button...but later I realized that I thought too simply. When the double-click event is triggered, the click will also be triggered~
After some research, I finally solved the problem by using the "setTimeout" delayed execution method in JS to delay the click execution for 300 milliseconds. The code is as follows:
Copy the code code as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http ://www.w3.org/1999/xhtml"><head>
<title></title>
<mce:script src="jquery-1.6.min.js" mce_src="jquery-1.6.min.js" type="text/javascript"></mce:script>
<mce:script type="text/javascript">
<!--
$(function () {
var num = 0;
var timeFunName = null;
$("button").bind("click", function () {
// Cancel the method that was not executed during the last delay
clearTimeout(timeFunName);
// Delay 300 milliseconds to execute click
timeFunName = setTimeout(function () {
num++;
$("textarea").val($("textarea").val() + "th" + num + "th event, event name: click/n");
}, 300); }).bind("dblclick", function () {
// Cancel the method that was not executed during the last delay
clearTimeout(timeFunName);
num++;
$("textarea").val($("textarea").val() + "th" + num + "th event, event name: double-click/n");
});
});
// --></mce:script>
</head>
<body>
<textarea rows="20" cols="50"></textarea><button type="button">Submit</button></body></html>