# Game countdown timer that works with BJS

**URL:** <https://forum.babylonjs.com/t/game-countdown-timer-that-works-with-bjs/12176>\
**Category:** Questions\
**Created:** [July 3, 2020, 4:35am UTC](https://forum.babylonjs.com/t/game-countdown-timer-that-works-with-bjs/12176 "2020-07-03T04:35:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mekaboo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mekaboo/32/440_2.png) [@Mekaboo](https://forum.babylonjs.com/u/Mekaboo)\
**Post date:** [July 3, 2020, 4:35am UTC](https://forum.babylonjs.com/t/game-countdown-timer-that-works-with-bjs/12176/1 "2020-07-03T04:35:57Z")

</div>

Hey!!

Im trying to find a simple timer that I can implement into my PG so that players can feel that they have a patch of time to finish the game I looked into this page:  
[Handle Events with Observables - Babylon.js Documentation](https://doc.babylonjs.com/how_to/observables) and saw a few but wanted to get feedback. Its the last piece of the puzzle(as far as the code part). Thank ya

[https://www.babylonjs-playground.com/#0XEE49#27](https://www.babylonjs-playground.com/#0XEE49#27)

💗 💗,  
Mekaboo

---

<div class="post-metadata">

**Author:** ![DisownedWheat](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@DisownedWheat](https://forum.babylonjs.com/u/DisownedWheat)\
**Post date:** [July 3, 2020, 7:15am UTC](https://forum.babylonjs.com/t/game-countdown-timer-that-works-with-bjs/12176/2 "2020-07-03T07:15:01Z")

</div>

You could just use `setInterval` if that solves your issue?

I must admit I have no experience with Babylon GUI so don’t judge me for that part 😛

[https://www.babylonjs-playground.com/#0XEE49#29](https://www.babylonjs-playground.com/#0XEE49#29)

---

<div class="post-metadata">

**Author:** ![DisownedWheat](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@DisownedWheat](https://forum.babylonjs.com/u/DisownedWheat)\
**Post date:** [July 3, 2020, 7:25am UTC](https://forum.babylonjs.com/t/game-countdown-timer-that-works-with-bjs/12176/3 "2020-07-03T07:25:06Z")

</div>

I just realised that maybe you did want to use observables specifically. Here’s how I’d go about doing that:  
[https://www.babylonjs-playground.com/#0XEE49#30](https://www.babylonjs-playground.com/#0XEE49#30)

Specifically here I’m using the `scene.beforeRenderObservable` to update the `count` variable every tick, subtracting the delta time between frames. Then just round the number and set it as the text in the GUI element.

---

<div class="post-metadata">

**Author:** ![Mekaboo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mekaboo/32/440_2.png) [@Mekaboo](https://forum.babylonjs.com/u/Mekaboo)\
**Post date:** [July 4, 2020, 2:59am UTC](https://forum.babylonjs.com/t/game-countdown-timer-that-works-with-bjs/12176/4 "2020-07-04T02:59:01Z")

</div>

This is neat and THANK YA SO VERY MUCH 💘(dont worry about the GUI Im still figuring it out as well 😆

Question: How do I get it to stop at 0? I noticed that it runs from positive numbers to negative…how do I get it to stop at 0(text saying times up or completed). Tried changing it but nothing happened.

[https://www.babylonjs-playground.com/#0XEE49#31](https://www.babylonjs-playground.com/#0XEE49#31)

BTW apologies for late response…work got in the way

---

<div class="post-metadata">

**Author:** ![DisownedWheat](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@DisownedWheat](https://forum.babylonjs.com/u/DisownedWheat)\
**Post date:** [July 4, 2020, 3:13am UTC](https://forum.babylonjs.com/t/game-countdown-timer-that-works-with-bjs/12176/5 "2020-07-04T03:13:47Z")

</div>

To get it to stop at 0 you’d just put a check on it before changing the GUI.

```
if (count <= 0) {
    count = 0;
}

```

See here: [https://www.babylonjs-playground.com/#0XEE49#32](https://www.babylonjs-playground.com/#0XEE49#32)

To say “Time’s Up” or something similar just change the text inside that `if` block and wrap the other part in an `else`. You might have to change the width of the label to accommodate it.

---

<div class="post-metadata">

**Author:** ![Mekaboo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mekaboo/32/440_2.png) [@Mekaboo](https://forum.babylonjs.com/u/Mekaboo)\
**Post date:** [July 4, 2020, 3:28am UTC](https://forum.babylonjs.com/t/game-countdown-timer-that-works-with-bjs/12176/6 "2020-07-04T03:28:15Z")

</div>

Thank ya Bunches​:heart: 😊
