# base/timer (vanilla.js 0.4.0)

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

쿨다운.

## Cooldown extends Object

쿨다운.
- "timer -= dt; if (timer <= 0) ..." 를 매번 손으로 쓰던 것을 표준화했다.
- activate() 로 발동하면 지정한 시간 동안 isReady() 가 거짓이 된다.

예제: 발동하고 시간이 지나 다시 준비되기

```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): 생성.
  - `duration` number: 쿨다운 시간. (초)
- `tick(timeDelta)` (method, since 0.3.0): 갱신.
  - `timeDelta` number: 지난 프레임과의 간격. (초, 남은 시간에서 빼고 0 아래로는 내려가지 않는다)
- `isReady()` (method, since 0.3.0): 발동 가능 여부.
  - returns boolean: 남은 시간이 0 이하이면 참.
- `activate()` (method, since 0.4.0): 발동. (준비되어 있으면 쿨다운을 시작하고 참을 반환, 러스트 판 activate 와 같다)
  - returns boolean: 발동했으면 참. (쿨다운 중이면 거짓, 남은 시간은 그대로)
- `reset()` (method, since 0.3.0): 곧바로 준비 상태로 되돌리기.
- `setDuration(duration)` (method, since 0.3.0): 쿨다운 시간 설정.
  - `duration` number: 쿨다운 시간. (초, 0 보다 작으면 0, 진행 중인 남은 시간은 바꾸지 않는다)
- `getDuration()` (method, since 0.3.0): 쿨다운 시간 반환.
  - returns number: 쿨다운 시간. (초)
- `getRemainSeconds()` (method, since 0.3.0): 남은 시간 반환.
  - returns number: 다시 준비될 때까지 남은 시간. (초, 준비되었으면 0)
- `getProgress()` (method, since 0.3.0): 진행 비율 반환. (0 = 방금 발동, 1 = 준비 완료)
  - returns number: 0 ~ 1. (쿨다운 시간이 0 이면 1, 쿨다운 중에 시간을 줄이면 0 보다 작아질 수 있다)

## RepeatTimer extends Object

반복 타이머.
- 일정 간격마다 콜백을 부른다. 프레임이 길어져 여러 번 지나쳤으면 밀린 만큼 몰아서 부른다.

예제: 간격마다 콜백 부르기

```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): 생성.
  - `interval` number: 간격. (초)
  - `elapsedEvent` Function: 간격마다 부르는 콜백.
- `tick(timeDelta)` (method, since 0.3.0): 갱신.
  - `timeDelta` number: 지난 프레임과의 간격. (초, 멈춰 있으면 쌓지 않는다, 한 번에 간격을 여러 번 넘으면 그 수만큼 콜백을 부른다)
- `start()` (method, since 0.3.0): 시작.
- `stop()` (method, since 0.3.0): 정지. (누적 시간도 비운다)
- `pause()` (method, since 0.3.0): 일시 정지.
- `setInterval(interval)` (method, since 0.3.0): 간격 설정.
  - `interval` number: 간격. (초, 0.000001 보다 작으면 0.000001)
- `getInterval()` (method, since 0.3.0): 간격 반환.
  - returns number: 간격. (초)
- `setElapsedEvent(elapsedEvent)` (method, since 0.3.0): 콜백 설정.
  - `elapsedEvent` Function: 간격마다 부르는 콜백. (인자 없음, null 이면 부르지 않는다)
- `isRunning()` (method, since 0.3.0): 동작 중 여부.
  - returns boolean: 동작 중이면 참. (만들 때부터 동작한다)
