js 防抖

作者: 叫我苏轼好吗 | 来源:发表于2019-05-28 10:40 被阅读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>

相关文章

网友评论

      本文标题:js 防抖

      本文链接:https://www.haomeiwen.com/subject/mnjrtctx.html