( interval: number, onInterval: () => void, onUpdate?: ( rate: number ) => void, limit?: number )
| 26 | * @param limit - Optional. Limits the number of interval. |
| 27 | */ |
| 28 | export function RequestInterval( |
| 29 | interval: number, |
| 30 | onInterval: () => void, |
| 31 | onUpdate?: ( rate: number ) => void, |
| 32 | limit?: number |
| 33 | ): RequestIntervalInterface { |
| 34 | const { now } = Date; |
| 35 | |
| 36 | /** |
| 37 | * The time when the interval starts. |
| 38 | */ |
| 39 | let startTime: number; |
| 40 | |
| 41 | /** |
| 42 | * The current progress rate. |
| 43 | */ |
| 44 | let rate = 0; |
| 45 | |
| 46 | /** |
| 47 | * The animation frame ID. |
| 48 | */ |
| 49 | let id: number; |
| 50 | |
| 51 | /** |
| 52 | * Indicates whether the interval is currently paused or not. |
| 53 | */ |
| 54 | let paused = true; |
| 55 | |
| 56 | /** |
| 57 | * The loop count. This only works when the `limit` argument is provided. |
| 58 | */ |
| 59 | let count = 0; |
| 60 | |
| 61 | /** |
| 62 | * The update function called on every animation frame. |
| 63 | */ |
| 64 | function update(): void { |
| 65 | if ( ! paused ) { |
| 66 | rate = interval ? min( ( now() - startTime ) / interval, 1 ) : 1; |
| 67 | onUpdate && onUpdate( rate ); |
| 68 | |
| 69 | if ( rate >= 1 ) { |
| 70 | onInterval(); |
| 71 | startTime = now(); |
| 72 | |
| 73 | if ( limit && ++count >= limit ) { |
| 74 | return pause(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | id = raf( update ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Starts the interval. |
| 84 | * |
| 85 | * @param resume - Optional. Whether to resume the paused progress or not. |
no outgoing calls
no test coverage detected
searching dependent graphs…