前端面试题:节流的实现( 减少一段时间的触发频率)

<!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>节流</title>
</head>
<body>
    <input type="text" />
    <input type="submit" id="input" />

    <script type="text/javascript">

        let btn  = document.getElementById('input')

        btn.addEventListener('click',throttle(submit,2000),false)

        function submit(params){
            console.log(params)
            console.log(this)
       
        }

        function throttle(fn,delay){
            let begin = 0 ;
            return function(e){
            
                let cur = new Date().getTime();
                console.log(cur-begin)
                if(cur - begin > delay){
                    fn.call(this,arguments)
                    begin = cur;
                }
            }
           

        }

    </script>
</body>
</html>

 

效果图

前端面试题:节流的实现( 减少一段时间的触发频率)

 

上一篇:CentOS学习笔记--基本命令--目录的相关操作


下一篇:Ubuntu中将网卡名称eno16777736改回eth0