1. 程式人生 > >web頁面中,按鈕長按三秒後執行一個方法

web頁面中,按鈕長按三秒後執行一個方法

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>長按三秒執行一個方法</title>
</head>
<body>
    <button>長按三秒執行一個方法</button>
    <script src="Scripts/jquery-3.3.1.js"></script>
<script> var up = true; var timeout = null; $(document).ready(function () { $("button").mousedown(function () { clearTimeout(timeout); up = false; timeout = setTimeout(function () { if
(!up) { alert("已經按了三秒了"); } }, 3000); }); $("button").mouseup(function () { clearTimeout(timeout); up = true; }); });
</script> </body> </html>