简体   繁体   English

JavaScript upTime函数-无法识别变量ID

[英]JavaScript upTime function - Not recognizing ID of variable

My goal is for calcyear (cyear) to initially start at the value "8". 我的目标是calcyear(cyear)最初从值“ 8”开始。 Every time the day count reaches 364, I want a value of "1" to be subtracted from this 8 value, until it reaches zero. 每天计数达到364时,我都希望从这8个值中减去“ 1”,直到达到零。

For some reason, the p id does not seem to be recognizing the calcyear id... Or perhaps it is, but the code is in error? 由于某些原因,p id似乎无法识别calcyear id。也许是,但是代码有错误?

Entire code: 完整代码:

<html>
<style>
#countup p {
display: inline-block;
padding: 0px;
margin: 0 0 10px;
}
#paragraph2 p {
display: inline-block;
padding: 0px;
margin: 0 0 10px;
}
</style>
<div id="countup">
  We are in day
  <p id="days">00</p>
  <p class="timeRefDays">of the calendar. This day has been going on for </p>
  <p id="hours">00</p>
  <p class="timeRefHours">hours, </p>
  <p id="minutes">00</p>
  <p class="timeRefMinutes">minutes, and </p>
  <p id="seconds">00</p>
  <p class="timeRefSeconds"> seconds.</p>
</div>

<div id="paragraph2">
<p>We are in month</p>
<p id="months">00</p>
<p>of the year.</p>
</div>

<div id="paragraph2">
<p>In </p>
<p id="calcyear">0</p>
<p> years an intercalation week will be added.</p>
</div>

<script>
window.onload=function() {
  upTime('mar,20,2016,00:00:00'); 
}
function upTime(countTo) {
  now = new Date();
  countTo = new Date(countTo);
  difference = (now-countTo);

  days=Math.floor(difference/(60*60*1000*24)*1);
  hours=Math.floor((difference%(60*60*1000*24))/(60*60*1000)*1);
  mins=Math.floor(((difference%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
  secs=Math.floor((((difference%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
  mons=Math.floor(difference/(24*60*60*1000*24)*1);

  cyear= 8
  years=Math.floor(days / 364)
  if (years > 1){ cyear = cyear - 1}

  document.getElementById('days').firstChild.nodeValue = days;
  document.getElementById('hours').firstChild.nodeValue = hours;
  document.getElementById('minutes').firstChild.nodeValue = mins;
  document.getElementById('seconds').firstChild.nodeValue = secs;
  document.getElementById('months').firstChild.nodeValue = mons;
  document.getElementById('years').firstChild.nodeValue = years;
  document.getElementById('calcyear').firstChild.nodeValue = cyear;


  clearTimeout(upTime.to);
  upTime.to=setTimeout(function(){ upTime(countTo); },1000);
}
</script>
</html>

There's no element with the id years , so your code stops there. 有id为没有元素years ,所以你的代码停在那里。

Just add <p id="years">0</p> to your code and it should run. 只需将<p id="years">0</p>到您的代码中,它就可以运行。

The problem is on this section of code: 问题在这段代码上:

document.getElementById('years').firstChild.nodeValue = years;

In your HTML there isn't an element with the id 'years' , as such you will receive the error: 在您的HTML中,没有ID为'years'的元素,因此您将收到错误消息:

Uncaught TypeError: Cannot read property 'firstChild' of null 未捕获的TypeError:无法读取null的属性“ firstChild”

Since there is no such element with the id 'years' , then document.getElementById('years') would return null . 由于没有id为'years'元素,因此document.getElementById('years')将返回null This error also stops execution, so the next lines would not run. 此错误也会停止执行,因此下一行将不会运行。 So because it's after that calcyear isn't updated. 因此,因为它是在calcyear之后才更新的。

document.getElementById('years').firstChild.nodeValue = years; // Stops
document.getElementById('calcyear').firstChild.nodeValue = cyear; // Not ran

It seems like you intended to add it but may of forgotten: 似乎您打算添加它,但可能会忘记:

<div id="paragraph2">
<p>We are in month</p>
<p id="months">00</p>
<p>of the year </p>
<p id="years">00</p>  <!-- Added line -->
</div>

Demo 演示版

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM