Can someone help me correct this code? It should be a two line countdown timer.

Joined
Jan 26, 2023
Messages
1
Reaction score
0
<style>#countdown{color:#000; display:inline-block; text-align:center; font-size:1.5rem}
#countdown>div{padding:10px; background:#E1CE2A; display:inline-block}
#countdown div>span{padding: 5px; display:inline-block} </style><center><div
id="countdown"><div><span class="days" id="day"></span><div
class="text">Days</div> </div> <div><span class="hours" id="hour"></span> <div
class="text">Hours</div> </div> <div><span class="minutes" id="minute"></span> <div
class="text"> Minutes</div></div><div> <span class="seconds" id="second">
</span><div class="text">Seconds</div></div></div> <p
id="timer"></p></left><script>var deadline=new Date("jan 1, 2022
15:37:25").getTime();var x=setInterval(function(){var now=new Date().getTime();var
t=deadline-now; var days=Math.floor(t/(1000*60*60*24)); var
hours=Math.floor((t%(1000*60*60*24))/(1000*60*60)); var
minutes=Math.floor((t%(1000*60*60))/(1000*60)); var
seconds=Math.floor((t%(1000*60))/1000);
document.getElementById("day").innerHTML=days;
document.getElementById("hour").innerHTML=hours;
document.getElementById("minute").innerHTML=minutes;
document.getElementById("second").innerHTML=seconds;if(t<0)
{clearInterval(x);document.getElementById("timer").innerHTML="TIME
UP";document.getElementById("day").innerHTML='0';
document.getElementById("hour").innerHTML='0';
document.getElementById("minute").innerHTML='0';
document.getElementById("second").innerHTML='0'}},1000); </script>
 
Joined
Jul 12, 2020
Messages
89
Reaction score
10
1. Please use code tags.
2. This is a javascript so it should be placed in the Javascript section of the forum.
3. When working out initial problems with a script of any kind, use only what is necessary. In this case it's only the spans that are being manipulated by the script. Do away with all styles, divisional containers etc.

I ran what you posted and it throws an "Unterminated String" error, but it's hard to know whether it's the actual problem with the code or that you didn't place the example in code tags.
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
This code is a countdown timer implemented in HTML, CSS, and JavaScript. It appears to be mostly correct, with the following observations:

  1. The HTML code contains some invalid syntax, such as the open tag for the <center> element not being closed, and some inconsistent indentation. The corrected HTML code is:
PHP:
<style>
  #countdown {
    color: #000;
    display: inline-block;
    text-align: center;
    font-size: 1.5rem;
  }
 
  #countdown > div {
    padding: 10px;
    background: #E1CE2A;
    display: inline-block;
  }
 
  #countdown div > span {
    padding: 5px;
    display: inline-block;
  }
</style>
<center>
  <div id="countdown">
    <div>
      <span class="days" id="day"></span>
      <div class="text">Days</div>
    </div>
    <div>
      <span class="hours" id="hour"></span>
      <div class="text">Hours</div>
    </div>
    <div>
      <span class="minutes" id="minute"></span>
      <div class="text">Minutes</div>
    </div>
    <div>
      <span class="seconds" id="second"></span>
      <div class="text">Seconds</div>
    </div>
  </div>
  <p id="timer"></p>
</center>

  1. The JavaScript code sets the deadline variable to a fixed date and time, which means the countdown timer will always count down to the same date. If you want the timer to count down to a different date, you need to modify the value of the deadline variable.
  2. The if statement at the end of the code block clears the interval timer and sets the contents of the HTML elements to "0" when the deadline has been reached. If you want to display a different message or change the behavior when the deadline has been reached, you can modify this part of the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,053
Messages
2,570,431
Members
47,075
Latest member
TysonV438

Latest Threads

Top