---
title: Timer
description: A timer that fires after a time interval has elapsed.
---

A timer that fires after a time interval has elapsed.

The timer fires after a specified time interval has elapsed. The timer can be repeating in which case it will fire multiple times.

## Properties

### repeats

Whether the timer should repeat.

```typescript
repeats: bool
```

A repeating timer will keep firing until it is invalidated. In contrast to non-repeating timers, repeating timers are not automatically invalidated. Defaults to false.

### timeInterval

The frequency at which the timer fires, in milliseconds.

```typescript
timeInterval: number
```

Be aware that the time interval is specified in setting. Defaults to 0, causing the timer to fire instantly.

## Methods

<span id="-new-timer" class="api-anchor"></span>

### new Timer()

Constructs a timer.

```typescript
new Timer()
```

Constructs a timer that fires after a specified time interval.

<span id="-invalidate" class="api-anchor"></span>

### invalidate()

Stops the timer from firing.

```typescript
invalidate()
```

Stops the timer from firing ever again. Non-repeating timers are automatically invalidated after they have fired once. Repeating timers must be manually invalidated.

<span id="-schedule" class="api-anchor"></span>

### schedule(callback)

Schedules the timer.

```typescript
schedule(callback: fn())
```

Schedules the timer using its configuration. The supplied function is called when the timer fires. To stop the timer from firing, call the `invalidate()` function.

#### Parameters

**`callback` `fn()`**

The callback to be called when the timer fires.

<span id="schedule" class="api-anchor"></span>

### schedule(timeInterval, repeats, callback)

Schedules a timer.

```typescript
static schedule(timeInterval: number, repeats: bool, callback: fn()): Timer
```

This is a convenience function for creating a new timer. The created timer is instantly scheduled and will fire after the specified time interval.

#### Parameters

**`timeInterval` `number`**

The time interval to fire the timer at.

**`repeats` `bool`**

Whether the timer should repeat or not.

**`callback` `fn()`**

The callback to be called when the timer fires.

#### Return value

**`Timer`**

The constructed timer.