根据错误状态更改元素的颜色代码

时间:2019-07-11 18:01:49

标签: javascript

我对这个特定的任务有些疯狂。基本上,我的状态会根据某些状态进行更新。状态还可以,小心,有危险。因此,如果可以,则颜色将为黑色。如果有任何类型的错误(即小心),则它会变成蓝色。如果它保持蓝色超过2分钟,它应该变成红色并继续变成红色,直到提供ok状态为止。如果随时将状态更新为“确定”,则应重置分钟计数器。

现在我面临的挑战是每秒更新一次,由于某种原因,我无法处理2分钟。现在,它显示出危险,但是随后要小心。

我的代码是:

   currentStatus = ''; // set by some other function
   previousStatus ='';
   displayedOn = 0;
   color ='black';
  function displayStatus() {

    const date = new Date();
    const now = date.getTime();

    switch (currentStatus) {
      case "ok":
      color ='black';

        if (this.previousSyncState == 'careful' || this.previousSyncState =='danger'){
          // previous status was an error of one kind so update
          // the last sync error reported then reset the timestamp
          localStorage.setItem('lastDate', date);
          displayedOn = now;
        }

        break;
      case "careful":
      case "danger":

        // has it been 30 minutes?

        var difference = now - displayedOn;
        var resultInMinutes = Math.round(difference / 60000);


        const minutesToWaitBeforeColorChange = 30;
        if (
         resultInMinutes >= minutesToWaitBeforeColorChange &&
          ["danger", "careful"].indexOf(previousStatus) > -1
        ) {
            // previously it was error and continueing to be same.
          displayedOn = now;
          this.isLateWarning = "red";
        } else {
            // it is either not 30 minutes on continue of an error
            // or previous it was ok
          this.isLateWarning = "blue";
        }

        break;
      default:
        this.isLateWarning = "black";
    }
  }

1 个答案:

答案 0 :(得分:0)

我不确定100%会遵循,但是我认为您想使用//not use Eigen defined variables double a[] = { 1,3,4,7 }, b[4]; adjacent_difference(a,a+4,b); for (int i = 0; i < 4; i++) cout << b[i] << endl; //use Eigen defined variables ArrayXd a1(4), b1(4); a1 << 1, 3, 4, 7; adjacent_difference(a1, a1 + 4, b1); for (int i = 0; i < 4; i++) cout << b1[i] << endl; 和一个标志来表明我们一直保持红色,直到获得setTimeout

ok