js節流函數進階版

來源:互聯網
上載者:User

標籤:delay   資料   發送   oct   his   UI   charset   ons   連續   

節流函數其主要作用就是防止使用者在短時間內多次觸發該事件。

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>Document</title></head><body>  思路: 滑鼠抬起,間隔0.5秒請求小區資料,通過上下箭頭選擇小區,輸入框文字變化,按下enter或者搜尋按鈕時,通過輸入框中小區名字擷取小區uid,如果沒有uid,就搜尋標題,如果有uid,通過uid擷取小區下的房來源資料,把資料發送到列表組件。   <script>  function throttle(fn, delay, duration) {    var timer = null,      begin = new Date();    return function() {      var context = this,        args = arguments,        current = new Date();      clearTimeout(timer);      if (current - begin >= duration) {        fn.apply(context, args);        begin = current;      } else {        timer = setTimeout(function() {          fn.apply(context, args);        }, delay)      }    }  };  function demo() {    console.log(1)  }  /*參數說明:第一個參數是要執行的函數,第二個參數是說在500毫秒內連續觸發了該函數只執行一次,第三個參數是說每隔200毫秒自動執行一次該函數*/  window.onresize = throttle(demo, 500, 200)  </script></body></html>

 

js節流函數進階版

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.