Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
How to run a JS delayed repeated function in update( ) with a conditional?

I'm developing a prototype game using framework Phaser with JavaScript. So, I'm trying to run a delayed function with a conditional var isPlaying == true in update() method, but it's not working. The function works, but not with the defined delayed time. It runs quickly and probably at FPS (deltaTime).

update(){

   if(isPlaying){

     this.time.addEvent({delay: 2500, callback: createItem, callbackScope: this, loop: true}); 

   }

}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The update function is fired at FPS, so you're adding a new event each frame. Each event that you add is with loop: true so it will call the callback function every 2500.

Depending on what you want to achieve, you may want to put the addEvent at the end of the create function instead of update and then unpause it only once in update.

var timer;

create() {
     timer = this.time.addEvent({delay: 2500, callback: createItem, callbackScope: this, loop: true, paused: true}); 
}

update(){

    if(isPlaying && timer.paused){
        timer.paused = false;
    }

}

You can find other note about Timers here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda