Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何永久地计算变量,并在用javscript完成条件时执行函数_Javascript_Typescript_Angular11 - Fatal编程技术网

Javascript 如何永久地计算变量,并在用javscript完成条件时执行函数

Javascript 如何永久地计算变量,并在用javscript完成条件时执行函数,javascript,typescript,angular11,Javascript,Typescript,Angular11,我从服务器上带来了一系列会议。该数组包含包含会议开始日期时间和会议结束日期时间的会议对象。我在模板中用ngx矩管显示了数组的所有项,该矩管显示了会议开始的剩余时间。我想做的是,当会议开始时间到达时,会显示一个按钮,可以加入会议。也许有某种方法可以永久地比较当前日期和会议开始日期,这样当会议到达时,我可以自动执行一个功能来显示按钮,无需重新加载页面?您可以创建一个值为true或false的observable,具体取决于是否应显示join按钮 private getShowButtonObserv

我从服务器上带来了一系列会议。该数组包含包含会议开始日期时间和会议结束日期时间的会议对象。我在模板中用ngx矩管显示了数组的所有项,该矩管显示了会议开始的剩余时间。我想做的是,当会议开始时间到达时,会显示一个按钮,可以加入会议。也许有某种方法可以永久地比较当前日期和会议开始日期,这样当会议到达时,我可以自动执行一个功能来显示按钮,无需重新加载页面?

您可以创建一个值为true或false的observable,具体取决于是否应显示join按钮

private getShowButtonObservable(m: Meeting): Observable<boolean> {
  const now = new Date();
  if (m.endDate.valueOf() < now.valueOf()) {
    // Meeting has ended, button will never show
    return of(false);
  } else if (m.startDate.valueOf() <= now.valueOf()) {
    // Meeting has started and is in progress
    // show button then hide it at endDate
    return timer(m.endDate).pipe(
      map(() => false), // When timer expires hide the button
      startWith(true) // Start with button shown
    );
  } else {
    // Meeting has not started
    const endTimer$ = timer(m.endDate).pipe(map(() => false));
    const startTimer$ = timer(m.startDate).pipe(map(() => true));
    return concat(startTimer$, endTimer$).pipe(startWith(false));
  }
}
private GetShowButtonObjectable(m:Meeting):可观察{
const now=新日期();
如果(m.endDate.valueOf()false));
const startTimer$=计时器(m.startDate).pipe(map(()=>true));
返回concat(startTimer$,endTimer$).pipe(starttith(false));
}
}
然后可以使用异步管道在HTML中显示/隐藏按钮。例如:

<div *ngFor="let meeting of meetings; let index = index">
  <h2> Meeting #{{ index + 1}} </h2>
   <strong>Start date:</strong> {{ meeting.startDate }}<br />
   <strong>End date: </strong> {{ meeting.endDate }}<br />
   <button *ngIf="meeting.showJoinButton$ | async">Join</button>
</div>

会议{{index+1}
开始日期:{{meeting.startDate}}
结束日期:{{meeting.endDate}}
参加

签出完整的工作示例。

您可以创建一个值为true或false的observable,具体取决于是否应显示join按钮

private getShowButtonObservable(m: Meeting): Observable<boolean> {
  const now = new Date();
  if (m.endDate.valueOf() < now.valueOf()) {
    // Meeting has ended, button will never show
    return of(false);
  } else if (m.startDate.valueOf() <= now.valueOf()) {
    // Meeting has started and is in progress
    // show button then hide it at endDate
    return timer(m.endDate).pipe(
      map(() => false), // When timer expires hide the button
      startWith(true) // Start with button shown
    );
  } else {
    // Meeting has not started
    const endTimer$ = timer(m.endDate).pipe(map(() => false));
    const startTimer$ = timer(m.startDate).pipe(map(() => true));
    return concat(startTimer$, endTimer$).pipe(startWith(false));
  }
}
private GetShowButtonObjectable(m:Meeting):可观察{
const now=新日期();
如果(m.endDate.valueOf()false));
const startTimer$=计时器(m.startDate).pipe(map(()=>true));
返回concat(startTimer$,endTimer$).pipe(starttith(false));
}
}
然后可以使用异步管道在HTML中显示/隐藏按钮。例如:

<div *ngFor="let meeting of meetings; let index = index">
  <h2> Meeting #{{ index + 1}} </h2>
   <strong>Start date:</strong> {{ meeting.startDate }}<br />
   <strong>End date: </strong> {{ meeting.endDate }}<br />
   <button *ngIf="meeting.showJoinButton$ | async">Join</button>
</div>

会议{{index+1}
开始日期:{{meeting.startDate}}
结束日期:{{meeting.endDate}}
参加

在上签出完整的工作示例。

它有效!非常感谢,伙计,我需要学习更多关于可观测的东西,这太神奇了!它起作用了!非常感谢,伙计,我需要学习更多关于可观测的东西,这太神奇了!