简体   繁体   English

使用setInterval时,变量不会在全局范围内更新

[英]Variable doesn't update in global scope when using setInterval

I want to write a function that will return a variable that I can use inside another function. 我想编写一个函数,该函数将返回一个可以在另一个函数中使用的变量。 Another function should use time comparison between current time and some other selected time. 另一个功能应该使用当前时间和其他选定时间之间的时间比较。 I figured I could make time variable accessible in global scope by using return and property and update it by using setInterval() . 我想可以通过使用return和property使时间变量在全局范围内可访问,并通过使用setInterval()对其进行更新。

Unfortunately when I "import" the variable to another function, the time variable isn't updated. 不幸的是,当我将变量“导入”到另一个函数时,时间变量没有更新。

Why doesn't console.log('time cool outside' + obj.debug ); 为什么不console.log('time cool outside' + obj.debug ); return the same value as console.log('time cool inside' + timeCool); 返回与console.log('time cool inside' + timeCool);相同的值console.log('time cool inside' + timeCool); ?

function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
    return i;
  }

function timeFunction() {
  let today = new Date();
  let hours = today.getHours();
  let minutes = today.getMinutes();
  minutes = checkTime(minutes);
  hours = checkTime(hours);
  let stringHours = hours.toString();
  let stringMinutes = minutes.toString();
  time = stringHours + stringMinutes;
  let timeCool = parseInt(time);
  console.log('time cool inside' + timeCool);
  return {
    debug: timeCool,
  };

}
let obj = timeFunction();
setInterval(timeFunction, 5000);
function a() {console.log('time cool outside' + obj.debug );}
setInterval(a, 5000);

in the lines 在行中

let obj = timeFunction();
setInterval(timeFunction, 5000);

you create a global object obj and set it to the output of timeFunction . 您创建一个全局对象obj并将其设置为timeFunction的输出。 Then with setInterval you call timeFunction periodically, but never set obj to its output. 然后使用setInterval定期调用timeFunction ,但不要将obj设置为其输出。 Thus, when a accesses obj it never changes 因此,当a访问obj它永远不会改变

Let's see what you are doing here: 让我们在这里看看你在做什么:

let obj = timeFunction();

Here you call the timeFunction and store it's result into obj . 在这里,您调用timeFunction并将其结果存储到obj

setInterval(timeFunction, 5000);

Here you call timeFunction every 5 seconds. 在这里,您每5秒调用一次timeFunction The return value is dismissed. 返回值被取消。

function a() {console.log('time cool outside' + obj.debug );}

Here you have a new function that references the obj variable defined before. 在这里,您有一个引用之前定义的obj变量的新函数。 obj is the return value of the first call to timeFunction and has never been changed, so it will always have the same value, no matter how often you call any of those two functions. obj是对timeFunction的第一次调用的返回值,并且从未更改,因此无论您调用这两个函数中的任何一个的频率如何,它始终具有相同的值。

If you want to update obj you need to do so inside timeFunction or continue to set it to the return value of timeFunction . 如果要更新obj ,则需要在timeFunction内部进行timeFunction或继续将其设置为timeFunction的返回值。

Thank you guys for helping. 谢谢你们的帮助。 Indeed I didn't notice that I didn't update "obj" variable. 确实,我没有注意到我没有更新“ obj”变量。

After implementing this: 实施此之后:

let obj;
function objUpdate() {
obj = timeFunction();
}
setInterval(objUpdate, 1000);

Everything looks like I wanted :) Thank you all! 一切看起来都像我想要的:)谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM