Skip to content

Commit

Permalink
docs(timer): add timer reference
Browse files Browse the repository at this point in the history
  • Loading branch information
lukadev-0 committed Feb 29, 2024
1 parent 66c7884 commit 5eff3b1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
],
},
],
Expand Down
1 change: 1 addition & 0 deletions docs/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
58 changes: 58 additions & 0 deletions docs/reference/timer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
outline: [2, 3]
---

<script setup>
import { data } from "./package-versions.data.ts";
</script>

# 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<number>
```

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.

0 comments on commit 5eff3b1

Please sign in to comment.