简体   繁体   English

如何将多个 function 值返回给多个全局变量?

[英]How do you return multiple function values to multiple global variables?

I'm trying to create a program where there are two variables x and y where they continually increment every 2 seconds.我正在尝试创建一个程序,其中有两个变量 x 和 y,它们每 2 秒连续递增一次。 2, 4, 6, 8, etc. I also want a reset button where once pressed, x and y become 0 and 1 respectively. 2、4、6、8 等。我还想要一个重置按钮,一旦按下,x 和 y 分别变为 0 和 1。

However the logic problem I'm having is how to get the function return values to be constantly assigned in a changing status to the global variables.然而,我遇到的逻辑问题是如何让 function 返回值在不断变化的状态下不断分配给全局变量。 I used an array with the name z to hold the x and y values and had x=z[0] and y=z[1], to transfer the multiple return values from the function. Like [a,b].我使用名称为 z 的数组来保存 x 和 y 值,并使用 x=z[0] 和 y=z[1] 来传输来自 function 的多个返回值。比如 [a,b]。

How do I continually tell the program that each time I increment x and y in my increase() function, that I want x and y to equal the new value?我如何不断地告诉程序每次我在 increase() function 中增加 x 和 y 时,我希望 x 和 y 等于新值? Because the current logic I'm trying to figure out should work once, but instead it doesn't work at all.因为我试图弄清楚的当前逻辑应该工作一次,但它根本不起作用。 Instead the values don't continually increase and x and y only increase once from 1 and 2 to 2 and 3 and never more.相反,这些值不会持续增加,x 和 y 只会从 1 和 2 增加到 2 和 3 一次,而不会更多。 The reset button doesn't work.重置按钮不起作用。 I don't know how to transfer the function return values to global values without the values being encapsulated in the function.我不知道如何将 function 返回值转换为全局值,而没有将值封装在 function 中。

 var x = 1; var y = 2; var z = [x, y]; x = z[0]; y = z[1]; function reset() { a = 0; b = 1; return [a, b]; } function increase(a, b) { a++; b++; return [a, b]; } function consoles(a, b) { console.log(a); console.log(b); } var clockTimer; clockTimer = setInterval(function() { z = increase(x, y); }, 2000); x = z[0]; y = z[1]; var consoleTimer; consoleTimer = setInterval(function() { consoles(z[0], z[1]); }, 2000);
 <input type="button" id="button" value="Reset" onclick="z=reset()"></input>

The problem is that when you do z = increase(x, y);问题是当你做z = increase(x, y); , you're passing the current value of x and y - but x and y only get assigned to once (well, twice, but the same thing is assigned both times): ,您传递的是xy的当前值 - 但xy只被分配一次(好吧,两次,但两次都分配了相同的东西):

x = z[0];
y = z[1];

If the z array changes, the values that x and y point to do not get changed accordingly.如果z数组更改,则xy指向的值不会相应更改。

If I were you, I'd choose one data structure, and ditch the other - either use an array, or use separate variables for each number, but not both, to avoid confusion like this:如果我是你,我会选择一种数据结构,而放弃另一种——要么使用数组,要么为每个数字使用单独的变量,但不能同时使用两者,以避免像这样混淆:

 var x = 1; var y = 2; function reset() { x = 0; y = 1; } function increase() { x++; y++; } setInterval(function() { z = increase(x, y); }, 2000); var consoleTimer; consoleTimer = setInterval(function() { console.log(x, y); }, 2000);
 <input type="button" id="button" value="Reset" onclick="z=reset()"></input>

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

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