diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index f40b4f4..73b3e47 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -49,6 +49,7 @@ export default defineConfig({ { text: "option", link: "/reference/option" }, { text: "result", link: "/reference/result" }, { text: "threadpool", link: "/reference/threadpool" }, + { text: "timer", link: "/reference/timer" }, ], }, ], diff --git a/docs/reference/index.md b/docs/reference/index.md index c6e493c..1a14a82 100644 --- a/docs/reference/index.md +++ b/docs/reference/index.md @@ -7,3 +7,4 @@ | [option](/reference/option) | Values that may or may not exist. | | [result](/reference/result) | Represent fallible operations. | | [threadpool](/reference/threadpool) | Spawn threads with reuse. | +| [timer](/reference/timer) | Schedule code to run at a future time or at an interval. | diff --git a/docs/reference/timer.md b/docs/reference/timer.md new file mode 100644 index 0000000..f2df6a1 --- /dev/null +++ b/docs/reference/timer.md @@ -0,0 +1,58 @@ +--- +outline: [2, 3] +--- + + + +# timer + +`timer` allows you to schedule code to run at a future time or at an interval. + +## Installation + +Learn more about [installation](/docs/getting-started#installation). + +::: code-group + +```toml-vue [Wally] +timer = "lukadev-0/timer@{{ data.timer }}" +``` + +```lua [Bundle] +local util = require(...) +util.timer.delay(...) +``` + +::: + +## Functions + +### timer.delay + +```lua +function timer.delay(duration: number): Future +``` + +Returns a future that resolves after the given duration in seconds, returning +the amount of time it took to resolve. + +This is equivalent to the following code: + +```lua +local fut = Future.spawn(task.wait, duration) +``` + +### timer.interval + +```lua +function timer.interval(duration: number, callback: function): () -> () +``` + +Calls the given callback every `duration` seconds. + +This will correct for drift, it repeatedly calls `task.wait` and calculates +the amount of time until the next call. + +Returns a function that will stop the interval when called.