简体   繁体   English

我有一个带有许多变量的 function。 如何在另一个 function 中使用它们?

[英]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);
    }
}

Edit编辑

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.

相关问题 我已经将Json数据嵌套存储在变量中,如何在jquery的另一个函数中访问它们? - I have nested Json data stored in variables how do I access them in another function in jquery? 如何使用JavaScript中另一个文件中的变量和函数? - How do I use variables and function from another file in javascript? 如何在另一个函数的函数中使用一个函数 - How do I use a function inside another function's function 如何使用概率并将它们插入我的其他 function? - How do I use probability and insert them into my other function? 如何使用JavaScript中另一个函数的变量 - How can I use variables from another function in javascript 我的函数以某种方式无法访问其父闭包并且缺少变量。怎么样? - My function somehow doesn’t have access to its parent closure & is missing variables. How? 如何在此重复函数中使用数组而不是变量? - How do I use an array instead of the variables in this repetitive function? 如何在我的函数中使用两个变量? - How do I use two variables in my function? 我如何在另一个函数上调用一个函数 - How do i call a function on another function Rollup 要我创建全局变量。 如何使用“导出默认值”? - Rollup wants me to create global variables. How can I use 'export default'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM