# countdown timer

```csharp
var countdown = 3
var countdownTaskId = 0

@Katton.command(name = "startcountdown", permission = "admin")
function startCountdown(sender, args) {
    countdown = 3

    countdownTaskId = Katton.every(1.seconds) {
        for (player in Katton.players.all()) {
            player.sendTitle("&e" + countdown, "")
        }

        countdown = countdown - 1

        if (countdown < 0) {
            Katton.cancel(countdownTaskId)
        }
    }
}
```

### showing "3, 2, 1, GO!" instead of "3, 2, 1, ..."

the version above will show `3`, `2`, `1`, `0`, and then stop (the cancel happens the tick *after* `0` is shown, so `0` still gets displayed). if you want a "GO!" message instead of showing `0` then do this:

```csharp
var countdown = 3
var countdownTaskId = 0

@Katton.command(name = "startcountdown", permission = "admin")
function startCountdown(sender, args) {
    countdown = 3

    countdownTaskId = Katton.every(1.seconds) {
        if (countdown > 0) {
            for (player in Katton.players.all()) {
                player.sendTitle("&e" + countdown, "")
            }
            countdown = countdown - 1
        } else {
            for (player in Katton.players.all()) {
                player.sendTitle("&aGO!", "")
            }
            Katton.cancel(countdownTaskId)
        }
    }
}
```

### adding a "stop early" command

since `countdownTaskId` is a top level `var`, any other function in the same script can reach it too so youcan include a manual stop command:

```csharp
@Katton.command(name = "stopcountdown", permission = "admin")
function stopCountdown(sender, args) {
    Katton.cancel(countdownTaskId)
    sender.sendMessage("&ccountdown stopped early")
}
```

### preventing a second countdown from starting while one is running

just like the colorswitch example, add a running flag:

```csharp
var countdown = 3
var countdownTaskId = 0
var countdownRunning = false

@Katton.command(name = "startcountdown", permission = "admin")
function startCountdown(sender, args) {
    if (countdownRunning) {
        sender.sendMessage("&ea countdown is already running")
        return
    }

    countdownRunning = true
    countdown = 3

    countdownTaskId = Katton.every(1.seconds) {
        if (countdown > 0) {
            for (player in Katton.players.all()) {
                player.sendTitle("&e" + countdown, "")
            }
            countdown = countdown - 1
        } else {
            for (player in Katton.players.all()) {
                player.sendTitle("&aGO!", "")
            }
            countdownRunning = false
            Katton.cancel(countdownTaskId)
        }
    }
}

@Katton.command(name = "stopcountdown", permission = "admin")
function stopCountdown(sender, args) {
    countdownRunning = false
    Katton.cancel(countdownTaskId)
    sender.sendMessage("&ccountdown stopped early")
}
```
