剩余时间等于X

时间:2015-12-26 01:38:21

标签: javascript logic

我有一个正在运行的js倒计时。当剩余时间变短时,我也会倒计时闪烁。

我想生成一组键号(用于何时闪烁)

我有这个



currenttime = Date.now() / 1000 | 0
targettime = currenttime + 20
flashfrequency = 3
flashenable = 10
setInterval(function() {
  currenttime = Date.now() / 1000 | 0
  remainingtime = targettime - currenttime
  if (remainingtime < 0)
    remainingtime = 0
  if ((remainingtime % flashfrequency === 0) && (remainingtime < flashenable))
    flash = "body {background-color:black; color:white;}"

  else
    flash = "body {background-color:white; color:black;}"

  document.getElementById('timehere').innerHTML = remainingtime;
  document.getElementById('flashhere').innerHTML = flash;

}, 50);
&#13;
<head>
  <style id="flashhere" type="text/css"></style>

  <body>
    <table>
      <tr>
        <td id="timehere">hello</td>
      </tr>
    </table>
  </body>
&#13;
&#13;
&#13;

我想用

var flashfrequency=10 //as time when to flash 

var flashenable=40 //as maximum remaining time to flash

我不知道从flashfrequency和flashhenable

开始生成值的位置

生成每个flash频率列表的代码,从0到0(0,10,20 ......)闪烁(... 30,40)。

if语句将剩余时间与生成列表(0,10,20,30,40)进行比较

1 个答案:

答案 0 :(得分:1)

尝试替换% Remainder operator

if (remainingtime % 10 === 0)

用于|| OR运营商

if ((remainingtime == 0) 
    || (remainingtime == 10) 
    || (remainingtime == 20) 
    || (remainingtime == 30) 
    || (remainingtime == 40))

if条件

&#13;
&#13;
currenttime = Date.now() / 1000 | 0
targettime = currenttime + 50

setInterval(function() {
  currenttime = Date.now() / 1000 | 0
  remainingtime = targettime - currenttime
  if (remainingtime < 0)
    remainingtime=0
  if (remainingtime % 10 === 0)
    flash = "body {background-color:black; color:white;}"

  else
    flash = "body {background-color:white; color:black;}"

  document.getElementById('timehere').innerHTML = remainingtime;
  document.getElementById('flashhere').innerHTML = flash;

}, 50);
&#13;
<head>
  <style id="flashhere" type="text/css"></style>

  <body>
    <table>
      <tr>
        <td id="timehere">hello</td>
      </tr>
    </table>
  </body>
&#13;
&#13;
&#13;