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

142
Visualizações
Javascript decrementing value returns NaN

Good day, I'm working on a small JS loop that executes an action each X second-s randomly selected in a range, here is my current script:

var test = {
    lastFrame: 0,
    timeBeforeSomething: 0.0,
    
    update: function() {
        let now = Date.now();
        
        if( this.lastFrame != undefined ) {
            let delta = ( now - this.lastFrame ) / 1000;
            
            if( this.timeBeforeSomething <= 0 ) {
                this.doSomething();
                
                this.timeBeforeSomething = Math.round( ( ( Math.random() * ( 1.0 - 0.5 ) ) + 0.5 ) * 1000 ) / 1000;
                console.log( 'this.timeBeforeSomething = ' + this.timeBeforeSomething );
            } else {
                this.timeBeforeSomething -= delta;
                console.log( 'this.timeBeforeSomething -= ' + delta + ' => ' + this.timeBeforeSomething );
            }
        }
        
        this.lastFrame = now;
        
        setTimeout( test.update, 0 );
    },
    
    doSomething: function() {
        console.log( 'something' );
    }
}

test.update();

My problem is coming from the timeBeforeSomething attribute, indeed this-one is set to NaN at the second loop.

But I only: assign to it float values, and decrement value, so I don't understand why.

Here is a trace of what this script does print:

something
this.timeBeforeSomething = 0.672
this.timeBeforeSomething -= 0.01 => NaN
this.timeBeforeSomething -= 0.004 => NaN
this.timeBeforeSomething -= 0.012 => NaN
this.timeBeforeSomething -= 0.013 => NaN
this.timeBeforeSomething -= 0.015 => NaN

And from there, timeBeforeSomething stay equal to 0.672, do you know why?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to bind your call to test.update to the current this context. Otherwise it will get the function's this context in which timeBeforeSomething is undefined:

setTimeout(test.update.bind(this), 1000);

Complete snippet:

var test = {
  lastFrame: 0,
  timeBeforeSomething: 0.0,

  update: function() {
    let now = Date.now();

    if (this.lastFrame != undefined) {
      let delta = (now - this.lastFrame) / 1000;

      if (this.timeBeforeSomething <= 0) {
        this.doSomething();

        this.timeBeforeSomething = Math.round(((Math.random() * (1.0 - 0.5)) + 0.5) * 1000) / 1000;
        console.log('this.timeBeforeSomething = ' + this.timeBeforeSomething);
      } else {
        this.timeBeforeSomething -= delta;
        console.log('this.timeBeforeSomething -= ' + delta + ' => ' + this.timeBeforeSomething);
      }
    }

    this.lastFrame = now;

    setTimeout(test.update.bind(this), 1000);
  },

  doSomething: function() {
    console.log('something');
  }
}

test.update();

about 4 years ago · Santiago Trujillo 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