forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
60 lines (55 loc) · 1.42 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { Ref } from 'vue-demi'
import { ref } from 'vue-demi'
import type { MaybeComputedRef, Pausable } from '../utils'
import { useIntervalFn } from '../useIntervalFn'
export interface UseIntervalOptions<Controls extends boolean> {
/**
* Expose more controls
*
* @default false
*/
controls?: Controls
/**
* Execute the update immediately on calling
*
* @default true
*/
immediate?: boolean
/**
* Callback on every interval
*/
callback?: (count: number) => void
}
/**
* Reactive counter increases on every interval
*
* @see https://vueuse.org/useInterval
* @param interval
* @param options
*/
export function useInterval(interval?: MaybeComputedRef<number>, options?: UseIntervalOptions<false>): Ref<number>
export function useInterval(interval: MaybeComputedRef<number>, options: UseIntervalOptions<true>): { counter: Ref<number> } & Pausable
export function useInterval(interval: MaybeComputedRef<number> = 1000, options: UseIntervalOptions<boolean> = {}) {
const {
controls: exposeControls = false,
immediate = true,
callback,
} = options
const counter = ref(0)
const update = () => counter.value += 1
const controls = useIntervalFn(callback
? () => {
update()
callback(counter.value)
}
: update, interval, { immediate })
if (exposeControls) {
return {
counter,
...controls,
}
}
else {
return counter
}
}