【问题标题】:Triger event whenever remaining time equals X每当剩余时间等于 X 时触发事件
【发布时间】:2015-12-26 01:38:20
【问题描述】:

我有一个正在运行的 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);
<head>
  <style id="flashhere" type="text/css"></style>

  <body>
    <table>
      <tr>
        <td id="timehere">hello</td>
      </tr>
    </table>
  </body>

我想用

var flashfrequency=10 //as time when to flash 

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

我不知道从哪里开始从 flashfrequency 和 flashenable 生成值

生成从 0 到 (0,10,20...) flashenable (...30,40) 的每个 flashfrequency'th 数字的列表。

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

【问题讨论】:

  • 可以创建stacksn-ps来演示吗?
  • @guest271314 制作了片段,并更改了 flash 变量以匹配 sn-p 示例
  • js at stacksn-ps 不返回预期结果?需求是什么?
  • 试图从中获得更多可用性,写 if ((remainingtime = 0) || (remainingtime = 10) || (remaini............ ..(剩余时间 = 590) || (剩余时间 = 600))

标签: javascript logic


【解决方案1】:

尝试替换% Remainder operator

if (remainingtime % 10 === 0)

对于||OR运营商

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

if 条件

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);
<head>
  <style id="flashhere" type="text/css"></style>

  <body>
    <table>
      <tr>
        <td id="timehere">hello</td>
      </tr>
    </table>
  </body>

【讨论】:

  • 感谢结合 '&& 剩余时间
猜你喜欢
  • 2019-05-28
  • 2021-03-17
  • 1970-01-01
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多