我一直试图弄清楚如何在我的网页中自动刷新时间(而且只是时间),但我似乎无法得到它。
这就是我所拥有的。
var d = new Date();
var weekday = d.getDay();
var day = d.getDate();
var month = d.getMonth() + 1; //JS says jan = 0
var year = d.getFullYear();
var minutes = d.getMinutes();
var hours = d.getHours() + 1; //eastern time zone
var seconds = d.getSeconds();
var ms = d.getMilliseconds();
function distime(minutes, hours, month, day, year) {
if (minutes < 10) {
var lowMin = "0" + minutes.toString();
document.getElementById("timecode").innerHTML = hours.toString() + ':' + lowMin + ' ' + month.toString() + '/' + day.toString() + '/' + year.toString();
alert("here");
} else
document.getElementById("timecode").innerHTML = hours.toString() + ':' + minutes + ' ' + month.toString() + '/' + day.toString() + '/' + year.toString();
alert("here");
};
// var clockTime = distime(minutes, hours, month, day, year);
function init(minutes, hours, month, day, year) {
alert("init");
distime(minutes, hours, month, day, year);
setTimeout(distime, 10000);
};
var initTime = init(minutes, hours, month, day, year);
时间附加到div,用HTML格式,我不使用PHP。
我听说我需要使用ajax来执行此操作,但我不确定如何实现它。
同样,我的问题是:如何每10秒刷新一次,以便在我的网站上正确显示。
谢谢!
解决我问题的更新代码如下所示。
var time = {};
(function () {
var clock = document.getElementById('timecode');
(function tick () {
var minutes, d = new Date();
time.weekday = d.getDay();
time.day = d.getDate();
time.month = d.getMonth() + 1; //JS says jan = 0
time.year = d.getFullYear();
time.minutes = d.getMinutes();
time.hours = d.getHours() + 1; //eastern time zone
time.seconds = d.getSeconds();
time.ms = d.getMilliseconds();
minutes = (time.minutes < 10 ? '0' + time.minutes : time.minutes);
clock.innerHTML = time.hours + ':' + minutes + ' ' + time.month + '/' + time.day + '/' + time.year;
window.setTimeout(tick, 1000);
}()); // Note the parens here, we invoke these functions right away
}()); // This one keeps clock away from the global scope
感谢所有帮助过的人!
答案 0 :(得分:5)
将它们移动到一个函数:
function updateTime() {
var d = new Date();
var weekday = d.getDay();
var day = d.getDate();
var month = d.getMonth() + 1; //JS says jan = 0
var year = d.getFullYear();
var minutes = d.getMinutes();
var hours = d.getHours() + 1; //eastern time zone
var seconds = d.getSeconds();
var ms = d.getMilliseconds();
distime(minutes, hours, month, day, year);
}
setInterval(updateTime, 10000);
答案 1 :(得分:0)
function updateTime() {
//Your function that prints time and replace the old time
// call this function again in 10000ms
setTimeout(updateTime, 10000); }
使用以下行在您的页面上调用此功能。
updateTime(); //initial call
希望它有所帮助。
答案 2 :(得分:0)
<div id="dvNow"></div>
<input type="button" value="Time" onclick="init()" />
<script type="text/javascript">
function init() {
setTimeout(distime, 1000);
setTimeout(init, 1000);
};
function distime() {
var now = new Date();
var sDateTime = now.toLocaleString();
$("#dvNow").html(sDateTime);
}
</script>
该功能仅更新Div#dvNow元素,无需刷新整页
答案 3 :(得分:0)
<script type="text/javascript">
init();
function init() {
setTimeout(distime, 1000);
setTimeout(init, 1000);
};
function distime() {
var now = new Date();
//var sDateTime = now.toLocaleString();
var sDateTime = now.toDateString() + " " + now.toLocaleTimeString()
$("#dvNow").html(sDateTime);
}
</script>
当页面加载时,init()函数将开始执行。日期和时间戳可以自定义。
答案 4 :(得分:0)
如果您需要程序其他部分的值,请将它们存储在某种全局状态,并在时钟函数中更新这些值。
在这个例子中,我们将所有部分存储在一个名为time
的对象中,这最小化了我们创建的全局变量的数量。然后我们可以在以后访问这些属性:time.ms
等。为了准确,您的时钟应该更频繁地滴答,然后不是 - 一次呼叫不会很难达到性能,并且会使您的分钟更准确一分钟。
另外需要注意的是,在JavaScript中为字符串添加数字时,数字会被强制转换为字符串,因此事先不需要调用.toString()
。
var time = {};
(function () {
var clock = document.getElementById('clock');
(function tick () {
var minutes, d = new Date();
time.weekday = d.getDay();
time.day = d.getDate();
time.month = d.getMonth() + 1; //JS says jan = 0
time.year = d.getFullYear();
time.minutes = d.getMinutes();
time.hours = d.getHours() + 1; //eastern time zone
time.seconds = d.getSeconds();
time.ms = d.getMilliseconds();
minutes = (time.minutes < 10 ? '0' + time.minutes : time.minutes);
clock.innerHTML = time.hours + ':' + minutes + ' ' + time.month + '/' + time.day + '/' + time.year;
window.setTimeout(tick, 1000);
}()); // Note the parens here, we invoke these functions right away
}()); // This one keeps clock away from the global scope
console.log(time.ms); // We have access to all those properties via a single variable.
&#13;
<div id="clock"></div>
&#13;