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

React裏面如何使用Throttle?

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

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

相關文章
相關標籤/搜索