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

83
Visualizações
Timer on the client side. Firebase + Angular

I'm doing an online auction. Accordingly, a timer is needed that, after the expiration of time, will change the status of the item to inactive. With the help of a web worker, I'm managed to do this. I check the end date (which I take from the firestore) with the current one and if they match, then I change the status. But unfortunately, it only works by pressing a button on the page that launches the webworker. If I change, for example, to onload, then ngOnInit does not have time to receive data from the firestore and, accordingly, the webworker is not executed. Is there a way that can help solve this problem?

I know that firebase has such a thing named 'function', but unfortunately it is paid.

component.ts

ngOnInit(){
this.initializeWorker();
}

timeWorker(){
    if(this.products.status==='active'){
      this.worker.postMessage(this.products.closureDate);

      this.worker.addEventListener('message', ({data})=>{
        this.checkStatus(data);
      })
    }
  }

  initializeWorker(){
    if (typeof Worker !== 'undefined') {
      // Create a new
      this.worker = new Worker(new URL('./random.worker', import.meta.url));
      this.worker.postMessage('');
    } else {
      // Web workers are not supported in this environment.
      // You should add a fallback so that your program still executes correctly.
    }
  }

worker.ts

addEventListener('message', ({ data }) => {
    console.log("data:" + data);
    const response = endUpAuc(data);
    postMessage(response);
});

function endUpAuc(ps) {
    console.log("ps: "+ps);
    var cDate = new Date(ps);
    console.log("cdate: "+cDate)
    if(cDate.getTime()>new Date().getTime())
    {
      console.log(cDate.getTime());
      console.log(new Date().getTime());
      console.log("active");
      return "active";
    }
    else if(cDate.getTime()<=new Date().getTime()){
      console.log("inactive");
      //this.updateStatus("inactive");
      return "inactive";
      //this.router.navigate(['/results'])
    }
}

component.html

<div class="about" onload="timeWorker()">
...
</div>
about 4 years ago · Juan Pablo Isaza
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