diff --git a/README.md b/README.md
index 1973a64..321620d 100755
--- a/README.md
+++ b/README.md
@@ -70,6 +70,10 @@ Vue.use(VueCompositionAPI);
- [`useMouseElement`](./src/components/useMouseElement/stories/useMouseElement.md) — tracks the mouse position relative to given element.
[](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-usemouseelement--demo)
- Animations
+ - [`useInterval`](./src/components/useInterval/stories/useInterval.md) — updates the `counter` value repeatedly on a fixed time delay.
+ [](https://microcipcip.github.io/vue-use-kit/?path=/story/animations-useinterval--demo)
+ - [`useIntervalFn`](./src/components/useIntervalFn/stories/useIntervalFn.md) — calls function repeatedly on a fixed time delay.
+ [](https://microcipcip.github.io/vue-use-kit/?path=/story/animations-useintervalfn--demo)
- [`useRaf`](./src/components/useRaf/stories/useRaf.md) — returns `elapsedTime` with requestAnimationFrame.
[](https://microcipcip.github.io/vue-use-kit/?path=/story/animations-useraf--demo)
- [`useRafFn`](./src/components/useRafFn/stories/useRafFn.md) — calls function with requestAnimationFrame.
diff --git a/src/components/useInterval/index.ts b/src/components/useInterval/index.ts
new file mode 100755
index 0000000..c617e4a
--- /dev/null
+++ b/src/components/useInterval/index.ts
@@ -0,0 +1 @@
+export * from './useInterval'
diff --git a/src/components/useInterval/stories/UseIntervalDemo.vue b/src/components/useInterval/stories/UseIntervalDemo.vue
new file mode 100755
index 0000000..ec0a8d6
--- /dev/null
+++ b/src/components/useInterval/stories/UseIntervalDemo.vue
@@ -0,0 +1,41 @@
+
+
+
+
+
Prop
+
Value
+
+
+
+
+
counter
+
+ {{ counter }}
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/components/useInterval/stories/useInterval.md b/src/components/useInterval/stories/useInterval.md
new file mode 100755
index 0000000..40457b5
--- /dev/null
+++ b/src/components/useInterval/stories/useInterval.md
@@ -0,0 +1,58 @@
+# useInterval
+
+Vue function that updates the `counter` value repeatedly on a fixed time delay.
+
+## Reference
+
+```typescript
+useInterval(
+ ms?: number,
+ runOnMount?: boolean
+): {
+ isRunning: Ref;
+ counter: Ref;
+ start: () => void;
+ stop: () => void;
+ };
+```
+
+### Parameters
+
+- `ms: number` how many milliseconds to wait before updating the counter
+- `runOnMount: boolean` whether to run the interval on mount, `true` by default
+
+### Returns
+
+- `isRunning: Ref` this value is `true` if the interval is running, `false` otherwise
+- `counter: Ref` the number of times the interval has run
+- `start: Function` the function used for starting the interval
+- `stop: Function` the function used for stopping the interval
+
+## Usage
+
+```html
+
+
+ `,
+ setup() {
+ const { isRunning } = useInterval(1000)
+ return { isRunning }
+ }
+})
+
+describe('useInterval', () => {
+ it('should show #isRunning when the intervals are called', async () => {
+ const wrapper = mount(testComponent())
+ jest.advanceTimersByTime(1500)
+
+ // Wait for Vue to append #isReady in the DOM
+ await wrapper.vm.$nextTick()
+ expect(wrapper.find('#isRunning').exists()).toBe(true)
+ })
+})
diff --git a/src/components/useInterval/useInterval.ts b/src/components/useInterval/useInterval.ts
new file mode 100755
index 0000000..634f8eb
--- /dev/null
+++ b/src/components/useInterval/useInterval.ts
@@ -0,0 +1,11 @@
+import { ref } from '@src/api'
+import { useIntervalFn } from '@src/vue-use-kit'
+
+export function useInterval(ms = 0, runOnMount = true) {
+ const counter = ref(0)
+ const animHandler = () => {
+ counter.value = counter.value + 1
+ }
+ const { isRunning, start, stop } = useIntervalFn(animHandler, ms, runOnMount)
+ return { isRunning, counter, start, stop }
+}
diff --git a/src/components/useIntervalFn/index.ts b/src/components/useIntervalFn/index.ts
new file mode 100755
index 0000000..3ad59a0
--- /dev/null
+++ b/src/components/useIntervalFn/index.ts
@@ -0,0 +1 @@
+export * from './useIntervalFn'
diff --git a/src/components/useIntervalFn/stories/UseIntervalFnDemo.vue b/src/components/useIntervalFn/stories/UseIntervalFnDemo.vue
new file mode 100755
index 0000000..8c4fd60
--- /dev/null
+++ b/src/components/useIntervalFn/stories/UseIntervalFnDemo.vue
@@ -0,0 +1,49 @@
+
+
+
+
+
Prop
+
Value
+
+
+
+
+
callbackCounter
+
+ {{ callbackCounter }}
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/components/useIntervalFn/stories/useIntervalFn.md b/src/components/useIntervalFn/stories/useIntervalFn.md
new file mode 100755
index 0000000..d716917
--- /dev/null
+++ b/src/components/useIntervalFn/stories/useIntervalFn.md
@@ -0,0 +1,63 @@
+# useIntervalFn
+
+Vue function that calls given callback repeatedly on a fixed time delay.
+
+## Reference
+
+```typescript
+useIntervalFn(
+ callback: Function,
+ ms?: number,
+ runOnMount?: boolean
+): {
+ isRunning: Ref;
+ start: () => void;
+ stop: () => void;
+ };
+```
+
+### Parameters
+
+- `callback: Function` the function to call for each interval finishes
+- `ms: number` how many milliseconds to wait before running the callback function
+- `runOnMount: boolean` whether to run the interval on mount, `true` by default
+
+### Returns
+
+- `isRunning: Ref` this value is `true` if the interval is running, `false` otherwise
+- `start: Function` the function used for starting the interval
+- `stop: Function` the function used for stopping the interval
+
+## Usage
+
+```html
+
+
- You can play with the animation with the play, pause, forward and backward buttons.
+ You can play with the animation with the 'play', 'pause' and 'invert direction' buttons.
You can also change the 'fps' value to make the horse run faster/slower.
diff --git a/src/components/useRafFn/useRafFn.spec.ts b/src/components/useRafFn/useRafFn.spec.ts
index edf9d05..2ffde0a 100755
--- a/src/components/useRafFn/useRafFn.spec.ts
+++ b/src/components/useRafFn/useRafFn.spec.ts
@@ -45,7 +45,7 @@ const testComponent = (onMount = false) => ({
})
describe('useRafFn', () => {
- it('should not display #isRunning when onMount is false', async () => {
+ it('should not show #isRunning when onMount is false', async () => {
const wrapper = mount(testComponent(false))
await wrapper.vm.$nextTick()
expect(rafSpy).not.toHaveBeenCalled()
@@ -53,7 +53,7 @@ describe('useRafFn', () => {
expect(wrapper.find('#elapsedTime').text()).toBe('0')
})
- it('should display #isRunning when onMount is true', async () => {
+ it('should show #isRunning when onMount is true', async () => {
const wrapper = mount(testComponent(true))
await wrapper.vm.$nextTick()
expect(rafSpy).toHaveBeenCalled()
diff --git a/src/components/useTimeout/stories/UseTimeoutDemo.vue b/src/components/useTimeout/stories/UseTimeoutDemo.vue
index f994538..7a7c800 100755
--- a/src/components/useTimeout/stories/UseTimeoutDemo.vue
+++ b/src/components/useTimeout/stories/UseTimeoutDemo.vue
@@ -14,14 +14,14 @@
-
+
-
- Cancel Timer
+
+ Stop Timer
@@ -38,23 +38,22 @@ export default Vue.extend({
name: 'UseTimeoutDemo',
setup() {
const timerDuration = 3000
- const { isReady, isIdle, cancel, start } = useTimeout(
+ const { isReady, start, stop } = useTimeout(
timerDuration,
false
)
const btnResetMsg = computed(() => {
- return isIdle.value ? 'Start timer' : 'Reset Timer'
+ return isReady.value === null ? 'Start timer' : 'Reset Timer'
})
const timerStatus = computed(() => {
- if (isIdle.value) return 'Idle'
if (isReady.value === false) return 'Pending...'
- if (isReady.value === null) return 'Cancelled'
+ if (isReady.value === null) return 'Idle'
return 'Completed'
})
- return { btnResetMsg, timerStatus, cancel, start }
+ return { btnResetMsg, timerStatus, start, stop }
}
})
diff --git a/src/components/useTimeout/stories/useTimeout.md b/src/components/useTimeout/stories/useTimeout.md
index 3e87ac7..4c75b43 100755
--- a/src/components/useTimeout/stories/useTimeout.md
+++ b/src/components/useTimeout/stories/useTimeout.md
@@ -10,9 +10,8 @@ useTimeout(
runOnMount?: boolean
): {
isReady: Ref;
- isIdle: Ref;
- cancel: () => void;
start: () => void;
+ stop: () => void;
}
```
@@ -26,10 +25,9 @@ useTimeout(
- `isReady: Ref` the timer status
- `false` when the timer is executing
- `true` when the timer is completed
- - `null` when the timer is cancelled
-- `isIdle: Ref` this value is `true` if the timer has ever been called, `false` otherwise
-- `cancel: Function` the function used to cancel the timer
+ - `null` when the timer is idle
- `start: Function` the function used for starting or resetting the timer
+- `stop: Function` the function used to stop the timer
## Usage
@@ -39,7 +37,7 @@ useTimeout(