<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>

</head>
<body>

<input type="text">
<script>
    var inp = document.querySelector('input');
    inp.addEventListener('input',throttle(function(){
        console.log(this.value);
    },1000));
    /* 
     函数节流:
     fn 要执行的函数
     delay 延迟间隔时间
     以1秒钟为例:如果上一次执行时间距离这一次执行时间间隔大于1秒钟则执行一次
     应用场景:拖拽,联想搜索,滚动窗口,放大缩小窗口
    */
    function throttle(fn,delay){
        var lastTime=new Date();
        return function(){
            var nowTime=new Date();
            if(nowTime-lastTime>delay){
                fn.call(this);
                lastTime = nowTime;
            }else{
                console.log('间隔时间太短了');
            }
            
        };
    };
</script>

</body>
</html>


kangting
62 声望3 粉丝

一只小小的前端