Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/use-interval/use-interval.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useRef } from "@rbxts/react";
import { setInterval } from "@rbxts/set-timeout";
import { useLatestCallback } from "../use-latest-callback";
import { BindingOrValue, getBindingValue } from "../utils/binding";

export interface UseIntervalOptions {
/**
Expand All @@ -19,14 +19,15 @@ export interface UseIntervalOptions {
* @param options The options for the interval.
* @returns A function that clears the interval.
*/
export function useInterval(callback: () => void, delay?: number, options: UseIntervalOptions = {}) {
export function useInterval(callback: () => void, delay?: BindingOrValue<number>, options: UseIntervalOptions = {}) {
const { immediate = false } = options;

const callbackMemo = useLatestCallback(callback);
const cancel = useRef<() => void>();
const cancel = useRef<thread>();

const clear = useCallback(() => {
cancel.current?.();
if (!cancel.current) return;
task.cancel(cancel.current);
}, []);

useEffect(() => {
Expand All @@ -36,7 +37,13 @@ export function useInterval(callback: () => void, delay?: number, options: UseIn
if (immediate) {
callbackMemo();
}
cancel.current = setInterval(callbackMemo, delay);
cancel.current = task.spawn(() => {
// eslint-disable-next-line no-constant-condition
while (true) {
task.wait(getBindingValue(delay));
callbackMemo();
}
});
return clear;
}, [delay]);

Expand Down