Javascript 模拟时钟不考虑当前时间 序言-我是编程新手,非常感谢您的帮助。

Javascript 模拟时钟不考虑当前时间 序言-我是编程新手,非常感谢您的帮助。,javascript,Javascript,我按照youtube指南编写了模拟时钟,但我的JavaScript函数并没有创建“实时”时钟。我已经检查了我的代码和视频,并纠正了一些差异,但我仍然无法让这个时钟正常工作 有人能帮我指出我哪里出了问题吗 作为快照附加的代码 Youtube指南: 代码: Javascript时钟 常数deg=6; const hr=document.querySelector(“#hr”); const mn=document.querySelector(“#mn”); const sc=document.q

我按照youtube指南编写了模拟时钟,但我的JavaScript函数并没有创建“实时”时钟。我已经检查了我的代码和视频,并纠正了一些差异,但我仍然无法让这个时钟正常工作

有人能帮我指出我哪里出了问题吗

作为快照附加的代码

Youtube指南:

代码:


Javascript时钟
常数deg=6;
const hr=document.querySelector(“#hr”);
const mn=document.querySelector(“#mn”);
const sc=document.querySelector(“#sc”);
设置间隔(()=>{
let day=新日期();
设hh=day.getHours()*30;
设mm=day.getMinutes()*deg;
设ss=day.getSeconds()*deg;
hr.style.transform='rotateZ(${hh+(mm/12)}度)';
mn.style.transform='rotateZ(${mm}deg)';
sc.style.transform='rotateZ(${ss}deg)';
})
到目前为止做得不错。 您可能需要查看的文档。您刚刚忘记了第二个参数(延迟)。您的代码应该如下所示:

setInterval(() => {
            let day = new Date();
            let hh = day.getHours() * 30;
            let mm = day.getMinutes() * deg;
            let ss = day.getSeconds() * deg;
            hr.style.transform = `rotateZ(${hh+(mm/12)}deg)`;
            mn.style.transform = `rotateZ(${mm}deg)`;
            sc.style.transform = `rotateZ(${ss}deg)`;
        }, 1000)
1000表示该函数将每1000毫秒调用一次,这将更新您的ui

编辑:根据

评论中的建议,合并了反勾号。到目前为止,做得不错。 您可能需要查看的文档。您刚刚忘记了第二个参数(延迟)。您的代码应该如下所示:

setInterval(() => {
            let day = new Date();
            let hh = day.getHours() * 30;
            let mm = day.getMinutes() * deg;
            let ss = day.getSeconds() * deg;
            hr.style.transform = `rotateZ(${hh+(mm/12)}deg)`;
            mn.style.transform = `rotateZ(${mm}deg)`;
            sc.style.transform = `rotateZ(${ss}deg)`;
        }, 1000)
1000表示该函数将每1000毫秒调用一次,这将更新您的ui


编辑:根据注释中的建议合并反勾号,例如,
setInterval(function(),1000),setInterval需要一个时间延迟作为第二个参数,如下所示:

因此,您的代码如下所示:


Javascript时钟
常数deg=6;
const hr=document.querySelector(“#hr”);
const mn=document.querySelector(“#mn”);
const sc=document.querySelector(“#sc”);
设置间隔(()=>{
let day=新日期();
设hh=day.getHours()*30;
设mm=day.getMinutes()*deg;
设ss=day.getSeconds()*deg;
hr.style.transform='rotateZ(${hh+(mm/12)}度)';
mn.style.transform='rotateZ(${mm}deg)';
sc.style.transform='rotateZ(${ss}deg)';
}, 1000);

setInterval需要一个时间延迟作为第二个参数,例如
setInterval(function(),1000)

因此,您的代码如下所示:


Javascript时钟
常数deg=6;
const hr=document.querySelector(“#hr”);
const mn=document.querySelector(“#mn”);
const sc=document.querySelector(“#sc”);
设置间隔(()=>{
let day=新日期();
设hh=day.getHours()*30;
设mm=day.getMinutes()*deg;
设ss=day.getSeconds()*deg;
hr.style.transform='rotateZ(${hh+(mm/12)}度)';
mn.style.transform='rotateZ(${mm}deg)';
sc.style.transform='rotateZ(${ss}deg)';
}, 1000);

这是一个基本时钟,请尝试了解它的工作原理,而不是重复youtupe指南。无意冒犯:P

function startTime() {
  var today = new Date();  
  //to get the the value of the current time of your country and apply it to "today".

  var h = today.getHours();  //"today" here is the var, it's not a constant.
  var m = today.getMinutes(); 
  var s = today.getSeconds();

  document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;

  var refresher = setTimeout(startTime, 500); 
  //the setTimeout here is to keep initiating the function as named in the first 
  //parameter and the second parameter is the time it waits before it initiates 
  //again, so basically the function is just to display the time at the moment
  //and the timeout is to keep showing it every second, try changing the number 
}


function checkTime(i) {
  if (i < 10) {i = "0" + i};  //this add zero in front of numbers < 10, youll get it 
                              //when learning the if statement.
  return i;
}
函数开始时间(){
var today=新日期();
//获取您所在国家当前时间的值并将其应用于“今天”。
var h=today.getHours();/“today”这里是var,它不是常数。
var m=today.getMinutes();
var s=today.getSeconds();
document.getElementById('txt').innerHTML=h+“:“+m+”:“+s;
var refresher=setTimeout(startTime,500);
//这里的setTimeout用于按照第一个函数中的名称继续启动函数
//参数,第二个参数是它在启动之前等待的时间
//同样,基本上这个函数只是显示当前的时间
//超时是保持每秒显示一次,尝试更改数字
}
功能检查时间(i){
如果(i<10){i=“0”+i};//在数字<10之前加上零,你就会得到它
//当学习if语句时。
返回i;
}

这是一个基本时钟,请尝试了解它的工作原理,而不是重复youtupe指南。无意冒犯:P

function startTime() {
  var today = new Date();  
  //to get the the value of the current time of your country and apply it to "today".

  var h = today.getHours();  //"today" here is the var, it's not a constant.
  var m = today.getMinutes(); 
  var s = today.getSeconds();

  document.getElementById('txt').innerHTML = h + ":" + m + ":" + s;

  var refresher = setTimeout(startTime, 500); 
  //the setTimeout here is to keep initiating the function as named in the first 
  //parameter and the second parameter is the time it waits before it initiates 
  //again, so basically the function is just to display the time at the moment
  //and the timeout is to keep showing it every second, try changing the number 
}


function checkTime(i) {
  if (i < 10) {i = "0" + i};  //this add zero in front of numbers < 10, youll get it 
                              //when learning the if statement.
  return i;
}
函数开始时间(){
var today=新日期();
//获取您所在国家当前时间的值并将其应用于“今天”。
var h=today.getHours();/“today”这里是var,它不是常数。
var m=today.getMinutes();
var s=today.getSeconds();
document.getElementById('txt').innerHTML=h+“:“+m+”:“+s;
var refresher=setTimeout(startTime,500);
//这里的setTimeout用于按照第一个函数中的名称继续启动函数
//参数,第二个参数是它在启动之前等待的时间
//同样,基本上这个函数只是显示当前的时间
//超时是保持每秒显示一次,尝试更改数字
}
功能检查时间(i){
如果(i<10){i=“0”+i};//在数字<10之前加上零,你就会得到它
//当学习if语句时。
返回i;
}

Javascript时钟
常数deg=6;
const hr=document.querySelector(“#hr”);
const mn=document.querySelector(“#mn”);
const sc=document.querySelector(“#sc”);
设置间隔(()=>{
let day=新日期();
设hh=day.getHours()*30;
设mm=day.getMinutes()*deg;
设ss=day.getSeconds()*deg;
hr.style.transform='rotateZ(+'(hh+(mm/12))+'deg';
mn.style.transform='rotateZ('+mm+'deg');
sc.style.transform='rotateZ('+ss+'deg');
}, 1000);

Javascript时钟