throttleTime
will start a new throttle interval (a time period in which no items will be emitted) when it receives a new value and isn't already throttled. The length of this throttle interval is determined by the duration you supply.
With RxJS 7 a new throttle interval is also started when a trailing value is emitted at the end of a throttle interval.
leading
and trailing
specify whether an item should be emitted at the beginning or end of a throttle interval.
leading:
Emit an item at the beginning of a new throttle interval.
trailing:
Emit the last item received from the source at the end of a throttle interval.
Visualisation
RxJS 6 & 7 - trailing: false
throttleTime(12 ticks, { leading: true, trailing: false })
source_1: --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval: --[~~~~~~~~~~~]----[~~~~~~~~~~~]---------[~~~~~~~~~~~]-----
output_1: --0----------------4---------------------8-----------------
source_2: --0--------1------------------2--------------3---4---------
throttle interval: --[~~~~~~~~~~~]---------------[~~~~~~~~~~~]--[~~~~~~~~~~~]-
output_2: --0---------------------------2--------------3-------------
throttleTime(12 ticks, { leading: false, trailing: false })
source_1: --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval: --[~~~~~~~~~~~]----[~~~~~~~~~~~]---------[~~~~~~~~~~~]-----
output_1: -----------------------------------------------------------
source_2: --0--------1------------------2--------------3---4---------
throttle interval: --[~~~~~~~~~~~]---------------[~~~~~~~~~~~]--[~~~~~~~~~~~]-
output_2: -----------------------------------------------------------
RxJS 6 - trailing: true
throttleTime(12 ticks, { leading: true, trailing: true })
source_1: --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval: --[~~~~~~~~~~~]----[~~~~~~~~~~~]---------[~~~~~~~~~~~]-----
output_1: --0-----------3----4-----------7---------8-----------9-----
source_2: --0--------1------------------2--------------3---4---------
throttle interval: --[~~~~~~~~~~~]---------------[~~~~~~~~~~~]--[~~~~~~~~~~~]-
output_2: --0-----------1---------------2--------------3-----------4-
throttleTime(12 ticks, { leading: false, trailing: true })
source_1: --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval: --[~~~~~~~~~~~]----[~~~~~~~~~~~]---------[~~~~~~~~~~~]-----
output_1: --------------3----------------7---------------------9-----
source_2: --0--------1------------------2--------------3---4---------
throttle interval: --[~~~~~~~~~~~]---------------[~~~~~~~~~~~]--[~~~~~~~~~~~]-
output_2: --------------1---------------------------2--------------4-
RxJS 7 - trailing: true
throttleTime(12 ticks, { leading: true, trailing: true })
source_1: --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval: --[~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~
output: --0-----------3-----------6-----------7-----------9--------
source_2: --0--------1------------------2--------------3---4---------
throttle interval: --[~~~~~~~~~~~I~~~~~~~~~~~]---[~~~~~~~~~~~]--[~~~~~~~~~~~I~
output_2: --0-----------1---------------2--------------3-----------4-
throttleTime(12 ticks, { leading: false, trailing: true })
source_1: --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval: --[~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~
output: --------------3-----------6-----------7-----------9--------
source_2: --0--------1------------------2--------------3---4---------
throttle interval: --[~~~~~~~~~~~I~~~~~~~~~~~]---[~~~~~~~~~~~I~~~~~~~~~~~]----
output_2: --------------1---------------------------2-----------4----