在线文档教程

JS实现长按事件功能

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div onmousedown="down()" onmouseup="up()" onclick="cli()">长按事件</div>
    <progress value="0" max="100"></progress>
    <!-- <button ontouchstart="down()" ontouchend="up()" onclick="cli()">点击</button> -->
    <script>
        let progress=document.getElementsByTagName('progress')[0];
        let time="";
        let lock=true;
        function down(){
            time=setInterval(()=>{
                lock=false;
                progress.value+=5
            },1000)
        }
        function up(){
           clearInterval(time);
           setTimeout(()=>{
               lock=true;
           })
        }
        function cli(){
            if(lock){
                alert(progress.value);
            }
        }
    </script>
</body>
</html>