Skip to content

Commit 93591c4

Browse files
authored
fix(useCountdown): start() should accept a custom initial value (#4554)
1 parent 88eefdb commit 93591c4

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

packages/core/useCountdown/index.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,27 @@ const { remaining, start, stop, pause, resume } = useCountdown(countdownSeconds,
2121
}
2222
})
2323
```
24+
25+
You can use a `ref` to change the initial countdown.
26+
`start()` and `resume()` also accept a new countdown value for the next countdown.
27+
28+
```js
29+
import { ref } from 'vue'
30+
import { useCountdown } from '@vueuse/core'
31+
32+
const countdown = ref(5)
33+
const { start, reset } = useCountdown(countdown, {
34+
})
35+
36+
// change the countdown value
37+
countdown.value = 10
38+
39+
// start a new countdown with 2 seconds
40+
start(2)
41+
42+
// reset the countdown to 4, but do not start it
43+
reset(4)
44+
45+
// start the countdown with the current value of `countdown`
46+
start()
47+
```

packages/core/useCountdown/index.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Pausable } from '@vueuse/shared'
22
import type { UseCountdownOptions } from '.'
33
import { beforeEach, describe, expect, it, vi } from 'vitest'
4-
import { effectScope } from 'vue'
4+
import { effectScope, ref } from 'vue'
55
import { useCountdown } from '.'
66

77
describe('useCountdown', () => {
@@ -110,4 +110,36 @@ describe('useCountdown', () => {
110110
vi.advanceTimersByTime(60)
111111
expect(tickCallback).toHaveBeenCalledTimes(0)
112112
})
113+
114+
it('initial interval can be changed', async () => {
115+
const countdown = ref(3)
116+
117+
const { start } = useCountdown(countdown, { ...options, immediate: false })
118+
119+
countdown.value = 2
120+
start()
121+
vi.advanceTimersByTime(210)
122+
expect(completeCallback).toHaveBeenCalledTimes(1)
123+
})
124+
125+
it('start can provide a custom interval', async () => {
126+
const { start, reset } = useCountdown(countdown, options)
127+
vi.advanceTimersByTime(countdown * interval + 10)
128+
expect(completeCallback).toHaveBeenCalledTimes(1)
129+
130+
start(1)
131+
vi.advanceTimersByTime(110)
132+
expect(completeCallback).toHaveBeenCalledTimes(2)
133+
134+
start()
135+
vi.advanceTimersByTime(110)
136+
expect(completeCallback).toHaveBeenCalledTimes(2)
137+
vi.advanceTimersByTime(countdown * interval + 10)
138+
expect(completeCallback).toHaveBeenCalledTimes(3)
139+
140+
start(1)
141+
reset()
142+
vi.advanceTimersByTime(110)
143+
expect(completeCallback).toHaveBeenCalledTimes(3)
144+
})
113145
})

packages/core/useCountdown/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ export interface UseCountdownReturn extends Pausable {
3232
/**
3333
* Resets the countdown and repeatsLeft to their initial values.
3434
*/
35-
reset: () => void
35+
reset: (countdown?: MaybeRefOrGetter<number>) => void
3636
/**
3737
* Stops the countdown and resets its state.
3838
*/
3939
stop: () => void
4040
/**
4141
* Reset the countdown and start it again.
4242
*/
43-
start: (initialCountdown?: MaybeRefOrGetter<number>) => void
43+
start: (countdown?: MaybeRefOrGetter<number>) => void
4444
}
4545

4646
/**
@@ -64,8 +64,8 @@ export function useCountdown(initialCountdown: MaybeRefOrGetter<number>, options
6464
}
6565
}, options?.interval ?? 1000, { immediate: options?.immediate ?? false })
6666

67-
const reset = () => {
68-
remaining.value = toValue(initialCountdown)
67+
const reset = (countdown?: MaybeRefOrGetter<number>) => {
68+
remaining.value = toValue(countdown) ?? toValue(initialCountdown)
6969
}
7070

7171
const stop = () => {
@@ -81,8 +81,8 @@ export function useCountdown(initialCountdown: MaybeRefOrGetter<number>, options
8181
}
8282
}
8383

84-
const start = () => {
85-
reset()
84+
const start = (countdown?: MaybeRefOrGetter<number>) => {
85+
reset(countdown)
8686
intervalController.resume()
8787
}
8888

0 commit comments

Comments
 (0)