返回

javascript——客户端的定时器。Firebase+角度

发布时间:2022-03-09 19:47:52 330
# 前端

我在网上拍卖。因此,需要一个计时器,该计时器在时间到期后将项目的状态更改为非活动状态。在一名网络工作者的帮助下,我成功地做到了这一点。我将结束日期(我从firestore获取)与当前日期进行检查,如果它们匹配,则更改状态。但不幸的是,它只能通过按下页面上启动webworker的按钮来工作。例如,如果我更改为onload,那么ngOnInit没有时间从firestore接收数据,因此不会执行webworker。有没有办法帮助解决这个问题?

我知道firebase有一个叫做“功能”的东西,但不幸的是它是付费的。

组成部分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.
    }
  }

工人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'])
    }
}

组成部分html


...
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像