简体   繁体   English

新文本到达时,不会删除javascript计时器完成后的文本

[英]Text after javascript timer finishes isn't removed when the new text arrives

https://jsfiddle.net/zLfuwdtu/1/ https://jsfiddle.net/zLfuwdtu/1/

I have a script that counts down a set date 'Date1'. 我有一个脚本,它会倒计时一个设置的日期“ Date1”。 While it counts down it displays a message "UNTIL FLOW". 倒数时,它会显示一条消息“ UNTIL FLOW”。 When that timer finishes it starts another timer 'Date2' in its place and displays "ON FLOW". 该计时器结束后,它将在其位置启动另一个计时器“ Date2”,并显示“ ON FLOW”。

The problem: When 'Date1' finishes the countdown, it continues to display the message from 'Date1' (UNTIL FLOW) alongside the 'Date2' message (ON FLOW). 问题:当“ Date1”完成倒计时时,它将继续显示“ Date1”(UNTIL FLOW)中的消息以及“ Date2”消息(ON FLOW)。 I need it to not display the message from 'Date1' when displaying the message from 'Date2'. 我需要它在显示来自“日期2”的消息时不显示来自“日期1”的消息。

  function getTimeRemaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor((t / 1000) % 60);
  var minutes = Math.floor((t / 1000 / 60) % 60);
  var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  var days = Math.floor(t / (1000 * 60 * 60 * 24));
  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}

function initializeClock(id, endtime, secondend, newfirstend) {
  var clock = document.getElementById(id);
  var daysSpan = clock.querySelector('.flowDays');
  var hoursSpan = clock.querySelector('.flowHours');
  var minutesSpan = clock.querySelector('.flowMinutes');
  var secondsSpan = clock.querySelector('.flowSeconds');

  function updateClock() {
    var t = getTimeRemaining(endtime);
    if(t.seconds<0)
        {
            clearInterval(timeinterval);
        }
    else
        {
    daysSpan.innerHTML = t.days;
    hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
    minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
    secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
        }
    if (t.total <= 0) {     
    document.getElementById("flowWindow").textContent= 'ON FLOW! ';
        endtime=secondend;
        }
    else 
    {document.getElementById("flow2Window").textContent= 'UNTIL FLOW';}
  }
  updateClock();
  var timeinterval = setInterval(updateClock, 1000);
}

var firstend = 'Sun Jul 05 2016 03:38:40 GMT-0400 (EDT)';
var secondend = 'Sun Jul 08 2016 20:52:10 GMT-0400 (EDT)';
initializeClock('flowClockdiv', firstend, secondend, firstend);
function getTimeRemaining(endtime) {
    var t = Date.parse(endtime) - Date.parse(new Date());
    var seconds = Math.floor((t / 1000) % 60);
    var minutes = Math.floor((t / 1000 / 60) % 60);
    var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
    var days = Math.floor(t / (1000 * 60 * 60 * 24));
    return {
        'total': t,
        'days': days,
        'hours': hours,
        'minutes': minutes,
        'seconds': seconds
    };
}

function initializeClock(id, flow) {
    var clock = document.getElementById(id);
    var daysSpan = clock.querySelector('.flowDays');
    var hoursSpan = clock.querySelector('.flowHours');
    var minutesSpan = clock.querySelector('.flowMinutes');
    var secondsSpan = clock.querySelector('.flowSeconds');

    function updateBoard(flow) {
        document.getElementById("flowWindow").textContent = '';
        document.getElementById("flow2Window").textContent = '';
        switch(flow[current].name)
        {
            case 'on':
                document.getElementById("flowWindow").textContent = 'ON FLOW! ';
                break;
            case 'until':
                document.getElementById("flow2Window").textContent = 'UNTIL FLOW';
                break;
        }
    }

    function updateClock() {
        var t = getTimeRemaining(flow[current].end);
        if (t.seconds >= 0) {
            daysSpan.innerHTML = t.days;
            hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
            minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
            secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);
        }
        if (t.total <= 0) {
            flow.shift();
            updateBoard(flow);
            if (flow[current].name == 'stop') {
                clearInterval(timer);
            }
        }
    }
    updateBoard(flow);
    var timer = setInterval(updateClock, 1000);
}
var current = 0;
var firstend = new Date();
var secondend = new Date();
firstend.setSeconds(firstend.getSeconds() + 5);
secondend.setSeconds(secondend.getSeconds() + 10);
var flow = [
    { 'name': 'until', 'end': firstend },
    { 'name': 'on', 'end': secondend },
    { 'name': 'stop', 'end': null }
];
initializeClock('flowClockdiv', flow);

Unless I've missed something important, I think you just need to hide each message whenever the opposite one needs to be visible: 除非我错过了重要的事情,否则我认为只要需要显示相反的消息,就只需隐藏每条消息:

if (t.total <= 0) {     
   document.getElementById("flowWindow").textContent= 'ON FLOW! ';
   document.getElementById("flow2Window").textContent= '';
   endtime=secondend;
}
else 
{
  document.getElementById("flow2Window").textContent= 'UNTIL FLOW';
   document.getElementById("flowWindow").textContent= '';
}

Or better still just use one single element for both messages: 或者更好的方法是,只对两个消息使用一个元素:

if (t.total <= 0) {     
   document.getElementById("flowWindow").textContent= 'ON FLOW! ';
   endtime=secondend;
}
else 
{
  document.getElementById("flowWindow").textContent= 'UNTIL FLOW';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 退出后,使用JavaScript进行的文本更改不会保留在弹出式窗口中 - Text Change with JavaScript isn't retained in popover after exiting 在javascript中设置计时器后更改文本 - Change the text after set the timer in javascript Javascript:使对象无效时不会删除setInterval - Javascript: setInterval isn't removed when nullifying an object 如何控制CSS伪元素:before和:after在使用javascript删除文本时消失 - How to control CSS pseudo elements :before and :after to disappear when text is removed using javascript 文字上的Javascript计时器 - Javascript timer on text 如果在 Javascript 超时后没有新消息到达,则运行触发器 - Run a trigger if no new message arrives after a timeout in Javascript JavaScript:如果只是删除选定的文本,则不会触发键事件 - JavaScript: Key event doesn't fire if selected text is just removed 使用新数据集时,为什么d3文本没有出现在条形图上? - Why isn't d3 text appearing on bar chart when using new dataset? 当 Javascript memory 游戏结束时,如何停止计时器计数? - How to stop a timer from counting when a Javascript memory game finishes? JavaScript 改变网页文本的背景颜色(当元素ID或名称不可用时) - Changing the background color of text on a webpage in JavaScript (when element ID or name isn't available)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM