# Performance concerns with multiple timers?

**URL:** <https://phaser.discourse.group/t/performance-concerns-with-multiple-timers/828>\
**Category:** Phaser 3\
**Created:** [January 19, 2019, 1:41am UTC](https://phaser.discourse.group/t/performance-concerns-with-multiple-timers/828 "2019-01-19T01:41:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JamesSkemp](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jamesskemp/32/47_2.png) [@JamesSkemp](https://phaser.discourse.group/u/JamesSkemp)\
**Post date:** [January 19, 2019, 1:41am UTC](https://phaser.discourse.group/t/performance-concerns-with-multiple-timers/828/1 "2019-01-19T01:41:10Z")

</div>

(Previously posted on the HTML 5 Game Dev forum, but since there were no responses and that’s going away, figured I’d move it over here.)

I’m working on a game where I want to do some checks every minute, every five minutes, and then every some amount of seconds (1 and 5 seconds now, but that might change).

Right now I’m ﻿adding the following in the main game scene:

```javascript
// Add a main timer that runs every second.
this.time.addEvent({
	delay: 1000,
	callback: this.doOneSecondActivities,
	callbackScope: this,
	loop: true
});
// Add a timer that runs every five seconds.
this.time.addEvent({
	delay: 5000,
	callback: this.doFiveSecondActivities,
	callbackScope: this,
	loop: true
});
// Add a timer that should run every minute.
this.time.addEvent({
	delay: 60000,
	callback: this.doOneMinuteActivities,
	callbackScope: this,
	loop: true
});
// Add a timer that should run every five minutes.
this.time.addEvent({
	delay: 1000 * 60 * 5,
	callback: this.doFiveMinuteActivities,
	callbackScope: this,
	loop: true
});

```

I realize that I shouldn’t prematurely optimize, but I’m wondering if I’m going to end up refactoring this later, or if it’s relatively safe to setup multiple timers in Phaser 3.

(The other way I can think of handling this is to determine what my smallest time is and then manually keep track of the last time the various events were triggered, adding/subtracting time to determine if an interval has passed. Since the game loop can be paused, I do realize that I might still need to check against a timestamp for certain things.)

Does anyone have experience with multiple timed events in Phaser 3?

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [January 19, 2019, 2:57am UTC](https://phaser.discourse.group/t/performance-concerns-with-multiple-timers/828/2 "2019-01-19T02:57:26Z")

</div>

I think 4 timers should be no problem at all.

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [January 19, 2019, 1:10pm UTC](https://phaser.discourse.group/t/performance-concerns-with-multiple-timers/828/3 "2019-01-19T13:10:52Z")

</div>

I agree with samme. The only overhead a Timer adds is a method call which checks every frame if it’s expired - that’s cheaper than even a Game Object, so 4 shouldn’t cause any problems at all.

---

<div class="post-metadata">

**Author:** ![asalinasci](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/asalinasci/32/1879_2.png) [@asalinasci](https://phaser.discourse.group/u/asalinasci)\
**Post date:** [August 27, 2020, 11:33am UTC](https://phaser.discourse.group/t/performance-concerns-with-multiple-timers/828/4 "2020-08-27T11:33:30Z")

</div>

With only one timer in a custom class the performance is very bad in a very simple game

---

<div class="post-metadata">

**Author:** ![BlunT76](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/blunt76/32/1454_2.png) [@BlunT76](https://phaser.discourse.group/u/BlunT76)\
**Post date:** [August 27, 2020, 1:08pm UTC](https://phaser.discourse.group/t/performance-concerns-with-multiple-timers/828/5 "2020-08-27T13:08:51Z")

</div>

Hi,  
Show us your timer, i use a lot of timer everywhere, without performance problems…but you must not use classic js timers

---

<div class="post-metadata">

**Author:** ![asalinasci](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/asalinasci/32/1879_2.png) [@asalinasci](https://phaser.discourse.group/u/asalinasci)\
**Post date:** [August 28, 2020, 2:23pm UTC](https://phaser.discourse.group/t/performance-concerns-with-multiple-timers/828/6 "2020-08-28T14:23:11Z")

</div>

I have made some changes in my simple game to not use timer and now the performance is better but still is not good. I have a callback to test the key pressed and sometimes the game stop for a while.It is strange because I don’t move anything on the screen and only change some text object.

This is the callback for the keydown event:

teclaPulsada(event){  
[//event.code=](https://event.code=) “KeyA”, event.keyCode= 65, key=“a”, which=65, console.log(event);

```
    let visibles=0, acierto=false;        
    
    //Compruebo si ha acertado
    for(let n=0; n<this.letreros.length; n++){
        let l=this.letreros[n];
        if(l.visible){
            visibles++;
            if(l.letra==event.key){
                visibles--;
                l.visible=false;
                this.imgs[l.numero].visible=false;
                //this.ponMas1(l.x, l.y);
                acierto=true;
            }
        }
    }
    
    //Si ya ha escrito todas las letras de esta fase
    if(acierto){
        this.aciertos++;            
        this.txtAciertos.setText("Aciertos: " + this.aciertos);
        
        if(this.aciertos>= this.maxAciertos){
            this.enMarcha=false;
            this.input.keyboard.removeAllListeners("keydown");
            this.panel.mostrar("¡Muy bien!", "Has superado el reto de Hexamano y has conseguido 10 golosinas telepáticas que acabamos de enviarte.\n\nAcabas de entrar en la lista de campeones de Mundo Vedoque")
        
        }else if(visibles==0){
            let usadas=aleatorios(6, this.teclas.length-1);
            
            for(let n=0; n<this.letreros.length; n++){
                let l=this.letreros[n];
                this.imgs[n].visible=true;
                l.visible=true;
                l.setText(this.teclas[usadas[n]]);
                l.letra=this.teclas[usadas[n]];
            }
            this.tiempoFase -= 1;   
            this.tiempo=this.tiempoFase;
            this.txtReloj.setText(this.tiempo);
        }
        
    }else{    
        //Si ha fallado le quito un acierto
        if(this.aciertos>0)
            this.aciertos--;            
        this.txtAciertos.setText("Aciertos: " + this.aciertos);
        this.tiempo = (this.tiempo>2) ? this.tiempo-2 : 0;
        this.txtReloj.setText(this.tiempo);
    }
}
```

---

<div class="post-metadata">

**Author:** ![BlunT76](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/blunt76/32/1454_2.png) [@BlunT76](https://phaser.discourse.group/u/BlunT76)\
**Post date:** [August 29, 2020, 9:08am UTC](https://phaser.discourse.group/t/performance-concerns-with-multiple-timers/828/7 "2020-08-29T09:08:54Z")

</div>

Is the game stopping on a particular key? If i remember well, some browsers extensions can cause troubles(evernote)
