# Timer

Kind: Class · Namespace: Sansar.Script · Assembly: Sansar.Script.dll · Script API 47.10.0

<div class="api-meta">
<span class="api-kind api-kind-class" data-pagefind-filter="kind:Class">Class</span>
<span data-pagefind-filter="namespace:Sansar.Script">Namespace <a href="/scripting/api/sansar.script"><code>Sansar.Script</code></a></span>
</div>

<span id="T:Sansar.Script.Timer"></span>

The Timer class is a static class that manages timer events for a script.

```csharp
public static class Timer
```

## Remarks

The time the event is generated is guaranteed to be no less than the given duration, but may be more depending on system load.

## Members at a glance

| Kind | Members |
|---|---|
| **Methods** | [Cancel](#cancel) · [Create](#create) |

## Methods

### Cancel

<div class="api-card">
<span id="M:Sansar.Script.Timer.Cancel(System.String)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Named timers are deprecated. Use the IEventSubscription returned from Timer.Create to cancel a timer.", false)]
public static void Cancel(string name)
```

Cancels a repeating timer.

| Parameter | Type | Description |
|---|---|---|
| `name` | [string](https://learn.microsoft.com/dotnet/api/system.string) | Name of the timer to cancel. |

<details class="api-remarks"><summary>Remarks</summary>

If no timer with the given name exists, a [`ArgumentException`](https://learn.microsoft.com/dotnet/api/system.argumentexception) will be thrown by the invocation.

</details>

</div>

### Create

<div class="api-card" id="create-double-action">
<span id="M:Sansar.Script.Timer.Create(System.Double,System.Action)"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 32.0.0</span></div>

```csharp signature
public static IEventSubscription Create(
    double initialDurationSeconds,
    Action handler
)
```

Generates a one time event.

| Parameter | Type | Description |
|---|---|---|
| `initialDurationSeconds` | [double](https://learn.microsoft.com/dotnet/api/system.double) | The minimum duration to elapse before the event is generated in seconds. |
| `handler` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | The [`Action`](https://learn.microsoft.com/dotnet/api/system.action) to be called when the time has elapsed. |

**Returns:** [IEventSubscription](/scripting/api/sansar.script/ieventsubscription)

</div>

<div class="api-card" id="create-timespan-action">
<span id="M:Sansar.Script.Timer.Create(System.TimeSpan,System.Action)"></span>

```csharp signature
public static IEventSubscription Create(
    TimeSpan initialDuration,
    Action handler
)
```

Generates a one time event.

| Parameter | Type | Description |
|---|---|---|
| `initialDuration` | [TimeSpan](https://learn.microsoft.com/dotnet/api/system.timespan) | The minimum duration to elapse before the event is generated. |
| `handler` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | The [`Action`](https://learn.microsoft.com/dotnet/api/system.action) to be called when the time has elapsed. |

**Returns:** [IEventSubscription](/scripting/api/sansar.script/ieventsubscription)

</div>

<div class="api-card" id="create-double-action-boolean">
<span id="M:Sansar.Script.Timer.Create(System.Double,System.Action,System.Boolean)"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 32.0.0</span></div>

```csharp signature
public static IEventSubscription Create(
    double initialDurationSeconds,
    Action handler,
    bool persistent
)
```

Generates a one time event.

| Parameter | Type | Description |
|---|---|---|
| `initialDurationSeconds` | [double](https://learn.microsoft.com/dotnet/api/system.double) | The minimum duration to elapse before the event is generated in seconds. |
| `handler` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | The [`Action`](https://learn.microsoft.com/dotnet/api/system.action) to be called when the time has elapsed. |
| `persistent` | [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | Set to true to continue generating events, false to stop after the first one |

**Returns:** [IEventSubscription](/scripting/api/sansar.script/ieventsubscription)

</div>

<div class="api-card" id="create-double-double-action">
<span id="M:Sansar.Script.Timer.Create(System.Double,System.Double,System.Action)"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 32.0.0</span></div>

```csharp signature
public static IEventSubscription Create(
    double initialDurationSeconds,
    double repeatDurationSeconds,
    Action handler
)
```

Generates a repeating event with an initial duration and a repeat duration.

| Parameter | Type | Description |
|---|---|---|
| `initialDurationSeconds` | [double](https://learn.microsoft.com/dotnet/api/system.double) | The minimum duration to elapse before the event is generated in seconds. |
| `repeatDurationSeconds` | [double](https://learn.microsoft.com/dotnet/api/system.double) | After the initial duration, events will be generated at this interval in seconds. |
| `handler` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | The [`Action`](https://learn.microsoft.com/dotnet/api/system.action) to be called when the time has elapsed. |

**Returns:** [IEventSubscription](/scripting/api/sansar.script/ieventsubscription)

</div>

<div class="api-card" id="create-timespan-action-boolean">
<span id="M:Sansar.Script.Timer.Create(System.TimeSpan,System.Action,System.Boolean)"></span>

```csharp signature
public static IEventSubscription Create(
    TimeSpan initialDuration,
    Action handler,
    bool persistent
)
```

Generates a one time event.

| Parameter | Type | Description |
|---|---|---|
| `initialDuration` | [TimeSpan](https://learn.microsoft.com/dotnet/api/system.timespan) | The minimum duration to elapse before the event is generated. |
| `handler` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | The [`Action`](https://learn.microsoft.com/dotnet/api/system.action) to be called when the time has elapsed. |
| `persistent` | [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | Set to true to continue generating events, false to stop after the first one |

**Returns:** [IEventSubscription](/scripting/api/sansar.script/ieventsubscription)

</div>

<div class="api-card" id="create-timespan-timespan-action">
<span id="M:Sansar.Script.Timer.Create(System.TimeSpan,System.TimeSpan,System.Action)"></span>

```csharp signature
public static IEventSubscription Create(
    TimeSpan initialDuration,
    TimeSpan repeatDuration,
    Action handler
)
```

Generates a repeating event with an initial duration and a repeat duration.

| Parameter | Type | Description |
|---|---|---|
| `initialDuration` | [TimeSpan](https://learn.microsoft.com/dotnet/api/system.timespan) | The minimum duration to elapse before the event is generated. |
| `repeatDuration` | [TimeSpan](https://learn.microsoft.com/dotnet/api/system.timespan) | After the initial duration, events will be generated at this interval. |
| `handler` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | The [`Action`](https://learn.microsoft.com/dotnet/api/system.action) to be called when the time has elapsed. |

**Returns:** [IEventSubscription](/scripting/api/sansar.script/ieventsubscription)

</div>

<div class="api-card" id="create-timespan-timespan-action-string">
<span id="M:Sansar.Script.Timer.Create(System.TimeSpan,System.TimeSpan,System.Action,System.String)"></span>
<div class="api-badges"><span class="api-badge api-deprecated">Deprecated</span></div>

```csharp signature
[System.Obsolete("Named timers are deprecated. Use the IEventSubscription returned from Timer.Create to cancel a timer.", false)]
public static void Create(
    TimeSpan initialDuration,
    TimeSpan repeatDuration,
    Action handler,
    string name
)
```

Generates a repeating event with an initial duration and a repeat duration. The event will continue as long as the script is active or until [`Timer.Cancel`](/scripting/api/sansar.script/timer#cancel) is called.

| Parameter | Type | Description |
|---|---|---|
| `initialDuration` | [TimeSpan](https://learn.microsoft.com/dotnet/api/system.timespan) | The minimum duration to elapse before the event is generated. |
| `repeatDuration` | [TimeSpan](https://learn.microsoft.com/dotnet/api/system.timespan) | After the initial duration, events will be generated at this interval. |
| `handler` | [Action](https://learn.microsoft.com/dotnet/api/system.action) | The [`Action`](https://learn.microsoft.com/dotnet/api/system.action) to be called when the time has elapsed. |
| `name` | [string](https://learn.microsoft.com/dotnet/api/system.string) | A short string to identify this Timer. |

<details class="api-remarks"><summary>Remarks</summary>

At most, one event will be generated per server frame, even if the duration is significantly shorter than the server frame time. Only the first 8 characters of the name are significant. Multiple timers with the same name are allowed. When [`Timer.Cancel`](/scripting/api/sansar.script/timer#cancel) is called the first timer with a matching name is stopped.

</details>

</div>


## Used in examples

- [BanHammerExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/BanHammerExample.cs)
- [ScriptLibrary/Quest/QuestObjectInteraction.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Quest/QuestObjectInteraction.cs)
- [ScriptLibrary/Quest/QuestObjectTrigger.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Quest/QuestObjectTrigger.cs)
- [ScriptLibrary/Scene/HintText.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/HintText.cs)
- [ScriptLibrary/Scene/Screenshot.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Screenshot.cs)
- [ScriptLibrary/Scene/Targeting.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Targeting.cs)
- [ScriptLibrary/Scene/TimeCountdown.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/TimeCountdown.cs)
- [ScriptLibrary/Scene/TimedEvents.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/TimedEvents.cs)
- [ScriptLibrary/ScriptWatcher.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/ScriptWatcher.cs)
- [SharedStopwatchExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/SharedStopwatchExample.cs)
- [SharedStopwatchReaderExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/SharedStopwatchReaderExample.cs)
- [TwitchEventExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/TwitchEventExample.cs)

<p class="api-generated" data-pagefind-ignore>Generated from the Sansar 47.10.0 client ScriptApi snapshot.</p>