Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在自行运行的html网页上设置时间函数?_Javascript_Html - Fatal编程技术网

Javascript 如何在自行运行的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(){

我想在自动运行的网页上设置时间,所以我有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(){
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
}