Hilbish/docs/api/hilbish/hilbish.timers.md

60 lines
1.6 KiB
Markdown
Raw Normal View History

---
title: Interface hilbish.timers
description: timeout and interval API
layout: doc
menu:
docs:
parent: "API"
---
## Introduction
2022-12-28 23:33:44 +00:00
If you ever want to run a piece of code on a timed interval, or want to wait
a few seconds, you don't have to rely on timing tricks, as Hilbish has a
timer API to set intervals and timeouts.
These are the simple functions `hilbish.interval` and `hilbish.timeout` (doc
accessible with `doc hilbish`). But if you want slightly more control over
them, there is the `hilbish.timers` interface. It allows you to get
a timer via ID and control them.
All functions documented with the `Timer` type refer to a Timer object.
An example of usage:
```
2023-01-18 10:39:26 +00:00
local t = hilbish.timers.create(hilbish.timers.TIMEOUT, 5000, function()
2022-12-28 23:33:44 +00:00
print 'hello!'
end)
t:start()
2023-01-18 10:39:26 +00:00
print(t.running) // true
2022-12-28 23:33:44 +00:00
```
2022-12-21 01:00:31 +00:00
## Interface fields
- `INTERVAL`: Constant for an interval timer type
- `TIMEOUT`: Constant for a timeout timer type
2023-01-18 10:39:26 +00:00
## Functions
### create(type, time, callback) -> <a href="/Hilbish/docs/api/hilbish/hilbish.timers/#timer" style="text-decoration: none;">Timer</a>
Creates a timer that runs based on the specified `time` in milliseconds.
The `type` can either be `hilbish.timers.INTERVAL` or `hilbish.timers.TIMEOUT`
### get(id) -> <a href="/Hilbish/docs/api/hilbish/hilbish.timers/#timer" style="text-decoration: none;">Timer</a>
Retrieves a timer via its ID.
## Types
## Timer
The Job type describes a Hilbish timer.
### Properties
- `type`: What type of timer it is
- `running`: If the timer is running
- `duration`: The duration in milliseconds that the timer will run
2023-01-18 10:39:26 +00:00
### Methods
#### start()
Starts a timer.
2023-01-18 10:39:26 +00:00
#### stop()
Stops a timer.