js 防抖

2019-05-28  本文已影响0人  叫我苏轼好吗
<!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>

</head>

<body>

      <input type="text" id="input">

        <script>

              var timer=null

                document.getElementById("input").addEventListener("keyup",function(){

                    console.log(this.value)

                      if(!timer){

                          clearTimeout(timer),

                          timer=setTimeout(()=>{    //使用箭头函数  如果是function this获取的是Windows的对象                                                                                      而不是 input的对象

                            console.log(this.value,'111111')

                          },1000)

                      }

                },false)//false 默认冒泡 true 默认捕获

                </script>

</body>

</html>
上一篇 下一篇

猜你喜欢

热点阅读