前端学习(1831):前端面试题之防抖

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    html,body{
        height: 500%;
    }
    </style>
</head>
<body>
    <button id="btn">歌谣</button>
    <script>
        function throwttle(fn,dalay){
            var timer=null;
            return function(){
                clearTimeout(timer);
        timer= setTimeout(function(){
            fn.apply(this);
        },dalay)
            }
           
        }
        document.getElementById('btn').onclick=throwttle(function(){console.log('scroll事件被触发了'+Date.now())},200)
    </script>
</body>
</html>

运行结果

猜你喜欢

转载自blog.csdn.net/weixin_43392489/article/details/107501806
今日推荐