[英]I have a function with many variables. How do I use them in another function?
var min = document.getElementById("minutes");
var sec = document.getElementById("second");
//sets a time interval to repeat.
setInterval(() => { //function() is the same as () => {} or () =>
let d = new Date(); //creating a variable to get the Time
let h = d.getHours(); //creating a variable to get hours and so on..
let m = d.getMinutes();
let s = d.getSeconds();
var Hours = 30*h+m/2+s/120;//calculating
var Minutes = 6*m+s/10;
var Seconds = 6*s;
hou.style.transform = `rotate(${Hours}deg)`;
min.style.transform = `rotate(${Minutes}deg)`;
sec.style.transform = `rotate(${Seconds}deg)`;
},1000)
function setAlarm(){
let h = document.getElementById("h");
let m = document.getElementById("m");
let s = document.getElementById("s");
if (h.innerHTML==Hours){
console.log(true);
}
}
I want to print true in my console if the value of the variable h in setAlarm() function is equal to the Hours variable in setInterval() .如果 setAlarm () function 中的变量h的值等于setInterval()中的Hours变量,我想在控制台中打印 true 。 How do I do that?
我怎么做?
I'm not positive this is what your'e asking but I'm pretty sure all you need to do is move the variable declarations out of the function like.我不肯定这是你的要求,但我很确定你需要做的就是将变量声明移出 function 之类的。
var min = document.getElementById("minutes");
var sec = document.getElementById("second");
let Hours;
//sets a time interval to repeat.
setInterval(() => { //function() is the same as () => {} or () =>
let d = new Date(); //creating a variable to get the Time
let h = d.getHours(); //creating a variable to get hours and so on..
let m = d.getMinutes();
let s = d.getSeconds();
Hours = 30*h+m/2+s/120;//calculating
var Minutes = 6*m+s/10;
var Seconds = 6*s;
hou.style.transform = `rotate(${Hours}deg)`;
min.style.transform = `rotate(${Minutes}deg)`;
sec.style.transform = `rotate(${Seconds}deg)`;
},1000)
function setAlarm(){
let h = document.getElementById("h");
let m = document.getElementById("m");
let s = document.getElementById("s");
if (h=Hours){
console.log(true);
}
}
As stated in the comments there are some issues with the code, here is my advice to you.如评论中所述,代码存在一些问题,这是我对您的建议。 Don't use duplicate identifier names in different functions as it will probably confuse you as a begginer.
不要在不同的函数中使用重复的标识符名称,因为这可能会使您作为初学者感到困惑。 Move all variable names that need accessed from multiple functions to the global scope.
将需要从多个函数访问的所有变量名称移动到全局 scope。
Just nope, but you can either expose the variable to upper scope or just invoke the setAlarm function in setInterval.不,但是您可以将变量公开给上 scope 或仅在 setInterval 中调用 setAlarm function。
var min = document.getElementById("minutes");
var sec = document.getElementById("second");
//sets a time interval to repeat.
setInterval(() => { //function() is the same as () => {} or () =>
let d = new Date(); //creating a variable to get the Time
let h = d.getHours(); //creating a variable to get hours and so on..
let m = d.getMinutes();
let s = d.getSeconds();
var Hours = 30*h+m/2+s/120;//calculating
var Minutes = 6*m+s/10;
var Seconds = 6*s;
hou.style.transform = `rotate(${Hours}deg)`;
min.style.transform = `rotate(${Minutes}deg)`;
sec.style.transform = `rotate(${Seconds}deg)`;
setAlarm(Hours);
},1000)
function setAlarm(Hours){
let h = document.getElementById("h");
let m = document.getElementById("m");
let s = document.getElementById("s");
if (h == Hours){
console.log(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.