最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get date to auto refresh? - Stack Overflow

programmeradmin5浏览0评论

I am trying to get a date to refresh on a page so I don't have to go and manually refresh it everyday. I have this code in place, but it doesn't seem to be working. The date displays, but it doesn't update when the day changes. For reference, this is being used on a BrightSign display. Can anyone tell me what I'm doing wrong? I'm kind of a JavaScript beginner so nothing too plex :)

<script type="text/javascript">
<!-- 
function clockTick()    {
   currentTime = new Date();
   month = currentTime.getMonth() + 1;
   day = currentTime.getDate();
   year = currentTime.getFullYear();
   setInterval(clockTick, 1000);
   return (month + "/" + day + "/" + year);
}
document.write(clockTick());
//-->
</script>

I am trying to get a date to refresh on a page so I don't have to go and manually refresh it everyday. I have this code in place, but it doesn't seem to be working. The date displays, but it doesn't update when the day changes. For reference, this is being used on a BrightSign display. Can anyone tell me what I'm doing wrong? I'm kind of a JavaScript beginner so nothing too plex :)

<script type="text/javascript">
<!-- 
function clockTick()    {
   currentTime = new Date();
   month = currentTime.getMonth() + 1;
   day = currentTime.getDate();
   year = currentTime.getFullYear();
   setInterval(clockTick, 1000);
   return (month + "/" + day + "/" + year);
}
document.write(clockTick());
//-->
</script>
Share Improve this question edited Dec 17, 2014 at 13:45 Legionar 7,6073 gold badges44 silver badges71 bronze badges asked Dec 17, 2014 at 13:37 Kristy SnyderKristy Snyder 331 gold badge2 silver badges5 bronze badges 2
  • 1 First you do not use document.write., second, learn about setTimeout or setInterval – epascarello Commented Dec 17, 2014 at 13:38
  • If you want the date to refresh, you have to poll it with an interval and actually change it when the day changes. Is that what you want ? – adeneo Commented Dec 17, 2014 at 13:40
Add a ment  | 

2 Answers 2

Reset to default 3

You will want to take the setInterval out of the function and set the time on the page inside the function so that it refreshes every second:

function clockTick() {
  var currentTime = new Date(),
      month = currentTime.getMonth() + 1,
      day = currentTime.getDate(),
      year = currentTime.getFullYear(),
      hours = currentTime.getHours(),
      minutes = currentTime.getMinutes(),
      seconds = currentTime.getSeconds(),
      text = (month + "/" + day + "/" + year + ' ' + hours + ':' + minutes + ':' + seconds);
  // here we get the element with the id of "date" and change the content to the text variable we made above
  document.getElementById('date').innerHTML = text;
}

// here we run the clockTick function every 1000ms (1 second)
setInterval(clockTick, 1000);
<span id="date"></span>

you can try this:call clockTick() from outside.

 function clockTick()    {
       currentTime = new Date();
       month = currentTime.getMonth() + 1;
       day = currentTime.getDate();
       year = currentTime.getFullYear();
      // alert("hi");
      document.getElementById('date').innerHTML=month + "/" + day + "/" + year;
    }
    
    setInterval(function(){clockTick();}, 1000);//setInterval(clockTick, 1000); will also work
<div id="date"></div>

发布评论

评论列表(0)

  1. 暂无评论