美文网首页
2018-01-05

2018-01-05

作者: 邪瓶张起灵 | 来源:发表于2018-02-08 20:10 被阅读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="test">
    <script>
    function debounce(fn, delay = 1000, time = 5000) {
    let timer = null
    let method = fn.bind(this)
    let startTime = new Date()
    return function (...args) {
    let curTime = new Date()

        clearTimeout(timer)
    
        if (curTime - startTime >= time) {
          method(args)
          startTime = new Date()
        }
        timer = setTimeout(function () {
          method(args)
        }, delay)
      }
    }
    
    function search(val) { console.log('search', val) }
    
    function onInput() {
      let value = test.value
      search(value)
    }
    
    let test = document.querySelector('#test')
    test.oninput = debounce(onInput)
    

    </script>
    </body>

    </html>

    相关文章

      网友评论

          本文标题:2018-01-05

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