Skip to content

Instantly share code, notes, and snippets.

@doggy8088
Last active September 6, 2018 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doggy8088/e324e393e4f29ce3275a04b616fdcafc to your computer and use it in GitHub Desktop.
Save doggy8088/e324e393e4f29ce3275a04b616fdcafc to your computer and use it in GitHub Desktop.

RxJS 與時間相關的 Operators 比較

有事件資料出現後,才會觸發 timer 並依據條件 emit 事件資料

debounceTime

*----@----------*----@
^
     *               *

throttleTime

*---*---*---@-----*----*-@
^                 ^
            *            *

auditTime

*---*---*---@-----*----*-@
        ^              ^
            *            *

一開始就會觸發 timer 並依據條件 emit 事件資料

bufferTime

--*---*---@----------@---***---@
        [*,*]       [ ]     [*,*,*]


windowTime

--*---*---@----------@---***---@
        Ob(**)     Ob()    Ob(***)

sampleTime

--*---*---@----------@---***---@
      ^                    ^
          *                    *

筆記心得

  • debounceTime:從最後一次 emit 後安靜一段時間(無發生任何 emit),才送出最後一次發生的值
  • throttleTime:從第一次 emit 發生後送出,並忽略設定時間(ms)內的其他 emit
  • auditTime:從第一次 emit 起一段時間後(ms),送出這段時間內最後一次 emit
  • bufferTime:收集一段時間(ms)內的 emit 後,以陣列型式 emit 收集到的結果
  • windowTime:收集一段時間(ms)內的 emit 後,以 Observable 型式 emit 收集到的結果
  • sampleTime:固定時間(ms)內送出最後一次 emit,如果時間內沒有任何 emit,則不會 emit

範例程式碼

延伸閱讀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment