React事件中的防抖


防抖函數有很多應用場景,比如輸入框change事件中請求后台數據, scroll事件加載更多等等一些高頻發生的事件中。。

loadsh中的throttle函數

Creates a throttled function that only invokes func at most once per every wait milliseconds. The throttled function comes with a cancel method to cancel delayed func invocations and a flush method to immediately invoke them. Provide options to indicate whether func should be invoked on the leading and/or trailing edge of the wait timeout. The func is invoked with the last arguments provided to the throttled function. Subsequent calls to the throttled function return the result of the last func invocation.
創建一個每wait毫秒最多只調用一次的函數。throttling函數帶有一個取消方法來取消延遲的func調用,還有一個刷新方法來立即調用它們。提供選項,以指示是否應該在等待超時的前邊緣和/或后邊緣調用func。使用提供給throttling函數的最后一個參數調用func。對throttling函數的后續調用返回上次func調用的結果。

React里面如何使用Throttle?

  1. 眾所周知, React里面的事件並非DOM的原生事件,並且它也沒有真正綁定在DOM元素上,它是一個React經過合成優化的對象,所以不用考慮里面屬性的兼容性
  2. react里面防抖

handleChange = (function(){
let func = lodash.throttle(function(e){
console.log(e.target.value);
}, 2000)
return function(e){
e.persist();
func(e);
}
})()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM