Estoy haciendo una subasta en línea. En consecuencia, se necesita un temporizador que, después de la expiración del tiempo, cambiará el estado del elemento a inactivo. Con la ayuda de un trabajador web, logré hacer esto. Compruebo la fecha de finalización (que tomo del almacén de incendios) con la actual y, si coinciden, cambio el estado. Pero desafortunadamente, solo funciona presionando un botón en la página que inicia el webworker. Si cambio, por ejemplo, a onload, entonces ngOnInit no tiene tiempo para recibir datos de Firestore y, en consecuencia, Webworker no se ejecuta. ¿Hay alguna manera que pueda ayudar a resolver este problema?
Sé que firebase tiene algo llamado 'función', pero desafortunadamente se paga.
componente.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. } }trabajador.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']) } }componente.html
<div class="about" onload="timeWorker()"> ... </div>