# base/timer (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/timer/

Cooldown.

## Cooldown extends Object

Cooldown.
- Standardizes the "timer -= dt; if (timer <= 0) ..." that was written by hand every time.
- When triggered with use(), isReady() becomes false for the specified time.

Example: Trigger and become ready again over time

```js
import { Cooldown } from "./build/vanilla.js";

const cooldown = new Cooldown(0.5);
console.log(cooldown.activate(), cooldown.activate());
cooldown.tick(0.25);
console.log(cooldown.getRemainSeconds(), cooldown.getProgress());
cooldown.tick(0.25);
console.log(cooldown.isReady());
```

- `constructor(duration = 1)` (constructor, since 0.3.0): Constructor.
  - `duration` number: Cooldown time. (seconds)
- `tick(timeDelta)` (method, since 0.3.0): Update.
  - `timeDelta` number: Interval since the last frame. (seconds, subtracted from the remaining time, which does not go below 0)
- `isReady()` (method, since 0.3.0): Whether it can be triggered.
  - returns boolean: True if the remaining time is 0 or less.
- `activate()` (method, since 0.4.0): Activates. (If ready, starts the cooldown and returns true, same as Rust activate)
  - returns boolean: True if triggered. (false during cooldown, the remaining time is kept)
- `reset()` (method, since 0.3.0): Immediately returns to the ready state.
- `setDuration(duration)` (method, since 0.3.0): Sets the cooldown time.
  - `duration` number: Cooldown time. (seconds, 0 if less than 0, does not change the remaining time in progress)
- `getDuration()` (method, since 0.3.0): Returns the cooldown time.
  - returns number: Cooldown time. (seconds)
- `getRemainSeconds()` (method, since 0.3.0): Returns the remaining time.
  - returns number: Time left until ready again. (seconds, 0 if ready)
- `getProgress()` (method, since 0.3.0): Returns the progress ratio. (0 = just triggered, 1 = ready)
  - returns number: 0 ~ 1. (1 if the cooldown time is 0, may go below 0 if the time is shortened during cooldown)

## RepeatTimer extends Object

Repeating timer.
- Calls the callback at every interval. If several intervals passed because a frame ran long, calls it for all the missed ones at once.

Example: Call a callback at each interval

```js
import { RepeatTimer } from "./build/vanilla.js";

let spawnCount = 0;
const repeatTimer = new RepeatTimer(0.5, () => {
	spawnCount += 1;
});
repeatTimer.tick(0.2);
repeatTimer.tick(1.1);
console.log(spawnCount);
repeatTimer.pause();
repeatTimer.tick(1);
console.log(spawnCount, repeatTimer.isRunning());
```

- `constructor(interval = 1, elapsedEvent = null)` (constructor, since 0.3.0): Constructor.
  - `interval` number: Interval. (seconds)
  - `elapsedEvent` Function: Callback called at every interval.
- `tick(timeDelta)` (method, since 0.3.0): Update.
  - `timeDelta` number: Interval since the last frame. (seconds, not accumulated while stopped, the callback is called once per interval passed at a time)
- `start()` (method, since 0.3.0): Start.
- `stop()` (method, since 0.3.0): Stops. (also clears the accumulated time)
- `pause()` (method, since 0.3.0): Pauses.
- `setInterval(interval)` (method, since 0.3.0): Sets the interval.
  - `interval` number: Interval. (seconds, 0.000001 if less than 0.000001)
- `getInterval()` (method, since 0.3.0): Returns the interval.
  - returns number: Interval. (seconds)
- `setElapsedEvent(elapsedEvent)` (method, since 0.3.0): Sets the callback.
  - `elapsedEvent` Function: Callback called at each interval. (no arguments, not called if null)
- `isRunning()` (method, since 0.3.0): Whether it is running.
  - returns boolean: True if running. (runs from creation)
