Javascript 如何在自行运行的html网页上设置时间函数?
我想在自动运行的网页上设置时间,所以我有id属性的html代码:Javascript 如何在自行运行的html网页上设置时间函数?,javascript,html,Javascript,Html,我想在自动运行的网页上设置时间,所以我有id属性的html代码: 函数tellTime(){ var now=新日期(); var theHr=now.getHours(); var theMin=now.getMinutes(); 写下(“时间:+theHr+:”+theMin); } document.getElementById(“demo”).innerHTML=tellTime() var myVar=setInterval(myTimer,1000); 函数myTimer(){
函数tellTime(){
var now=新日期();
var theHr=now.getHours();
var theMin=now.getMinutes();
写下(“时间:+theHr+:”+theMin);
}
document.getElementById(“demo”).innerHTML=tellTime()代码>
var myVar=setInterval(myTimer,1000);
函数myTimer(){
var d=新日期();
document.getElementById(“demo”).innerHTML=d.toLocaleTimeString();
}
此页面上的脚本启动此时钟:
停止时间
使用:
您可以使用自动刷新或创建事件(例如单击事件)来重新加载时间,使用setTimeout,您可以每秒刷新一次时间,如下所示:
function tellTime() {
var now = new Date();
var theHr = now.getHours();
var theMin = now.getMinutes();
return "time: " + theHr + ":" + theMin;
}
function printTime() {
document.getElementById("demo").innerHTML = tellTime();
setTimeout(printTime, 1000); // Run this function again after a second
}
您需要使用setInterval
函数tellTime(){
var now=新日期();
var theHr=now.getHours();
var theMin=now.getMinutes();
var theSec=now.getSeconds();
返回时间:“+theHr+”:“+theMin+”:“+theSec;
}
setInterval(函数(){
document.getElementById(“demo”).innerHTML=tellTime();
}, 1000);代码>
避免链接到学校。最好链接到质量更好的在线资源,如MDN或W3。
function tellTime() {
var now = new Date();
var theHr = now.getHours();
var theMin = now.getMinutes();
return "time: " + theHr + ":" + theMin;
}
function printTime() {
document.getElementById("demo").innerHTML = tellTime();
setTimeout(printTime, 1000); // Run this function again after a second
}