如何获取自动刷新的日期
How to get date to auto refresh?
我正在尝试在页面上获取刷新日期,这样我就不必每天手动刷新它。我有这段代码,但它似乎不起作用。日期会显示,但日期更改时不会更新。作为参考,这正在BrightSign显示器上使用。谁能告诉我我做错了什么?我是一个JavaScript初学者,所以没有什么太复杂的:)
<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>
您需要从
函数中取出setInterval
,并在函数内的页面上设置时间,以便它每秒刷新一次:
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>
你可以试试这个:从外面打电话clockTick()
。
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>
相关文章:
- 强制模板刷新ember.js
- 如何通过ajax刷新JSF填充的javascript变量
- 比较从函数和生成的日期对象
- 使用年、月、日期、小时、秒格式每分钟刷新一次图像
- Jquery 日期选择器仅在页面刷新后工作
- 如何在不刷新浏览器的情况下更新倒计时达到 0 时的日期
- 刷新日期更改的完整日历视图
- 如何获取自动刷新的日期
- 如何自动捕获(系统)日期更改并用javascript刷新页面
- 即使在刷新后也要记住日期选择器中选择的日期
- 在通过函数将数字转换为日期并加载刷新更多数据中
- Angularjs自动刷新momentjs-fromnow日期值
- Knockoutjs的日期时间字段更新不会刷新模型视图
- jQuery UI Datepicker -防止刷新日期点击
- JQuery日期选择器在刷新时不显示
- 在angular中刷新日期时间选择器
- 保存所选日期从日期选择器甚至页面刷新
- 刷新页面后,jQuery日期选择器不起作用
- 刷新页面时,日历中的日期设置回当前日期
- 如何使用javascript根据特定日期和时间刷新页面