[英]Code is getting stuck somewhere in a succession of for-loops and I'm not sure why
EDIT - I changed the code to correctly declare variables below but nothing seems to have changed 编辑-我更改了代码以正确地在下面声明变量,但似乎什么都没有改变
I've written code using a for-loop that has to satisfy a number of criteria before executing what's within it. 我已经使用for循环编写了代码,该循环必须先执行许多条件,然后才能执行其中的内容。 The problem is that, somewhere along the way, the code is getting stuck inside one of the loops, causing the computer to crash.
问题在于,在此过程中,某个地方的代码被卡在了其中一个循环中,从而导致计算机崩溃。
I've tried breaking the loop but this doesn't seem to help. 我尝试打破循环,但这似乎无济于事。
function compareKeypoints(varifiedKeypoints) {
outer_loop: for (i = 0; i < varifiedKeypoints.length; i++) {
let initialKeypoint = varifiedKeypoints[i];
for (j = 0; j < varifiedKeypoints.length; j++) {
let comparisonKeypoint = varifiedKeypoints[j];
if (initialKeypoint.part != comparisonKeypoint.part) {
if (Math.abs(comparisonKeypoint.position.x - initialKeypoint.position.x) <= 20
&& Math.abs(comparisonKeypoint.position.y - initialKeypoint.position.y) <= 20) {
if (keypointsCompatible(initialKeypoint.part, comparisonKeypoint.part)) {
console.log("Activating part: " + initialKeypoint.part);
console.log("Activated part: " + comparisonKeypoint.part);
let keypointPair = {
point_1: initialKeypoint.part,
point_2: comparisonKeypoint.part
}
console.log("Pushing parts!");
activeParts.push(keypointPair);
console.log("breaking loop!");
break outer_loop;
console.log("Loop NOT broken!!");
}
}
}
}
}
if (activeParts.length > 0) {
console.log(activeParts);
}
}
function keypointsCompatible(keypoint_1, keypoint_2) {
var outcome = true;
if (activeParts.length > 0) {
compatibility_loop: for (i = 0; i < activeParts.length; i++) {
if (Object.values(activeParts[i]).includes(keypoint_1) && Object.values(activeParts[i]).includes(keypoint_2)) {
console.log(keypoint_1 + " and " + keypoint_2 + " are not compatible because they already exist as " + activeParts[i].point_1 + " and " + activeParts[i].point_2 + " respectively");
outcome = false;
break compatibility_loop;
console.log("Compatibility NOT broken!!");
}
}
}
console.log("Compatibility outcome is " + outcome);
return outcome;
}
The code is suppose to take two values in the same array and compare them. 该代码假定在同一数组中接受两个值并进行比较。 If a number of conditions are met, including if they're a certain distance apart from one another, they will be pushed into a secondary array.
如果满足许多条件,包括彼此之间有一定距离,则将它们推入辅助阵列。 If the values already appear in the secondary array, which the keypointCompatible function is suppose to determine, the loop should either continue looking for other candidates or stop before being called again.
如果值已经出现在辅助数组中(假定keypointCompatible函数可以确定),则循环应继续寻找其他候选对象,或者在再次调用之前停止。 For some reason, however, the code is getting stuck within the keypointCompatible function when it detects that the values have already appeared in the secondary array and the console will repeatedly print "Compatibility is false" until the browser crashes.
但是由于某种原因,当代码检测到值已出现在辅助数组中时,该代码就会卡在keypointCompatible函数中,并且控制台将反复打印“ Compatibility is false”,直到浏览器崩溃为止。
Use let
or const
instead of var
or nothing. 使用
let
或const
代替var
或不使用。 Your issue may be related to closures and variables reused between loops. 您的问题可能与闭包和循环之间重用的变量有关。 Make sure you use
let
or const
in your loops too. 确保在循环中也使用
let
或const
。 for (let i=0)
. for (let i=0)
。
When you use let
or const
, the runtime will create a new instance every time the block or loop iterates. 使用
let
或const
,每次块或循环迭代时,运行时都会创建一个新实例。 However, using var
will reuse the internal allocation. 但是,使用
var
将重用内部分配。
So what happens with the standard var
is the multiple closures or loops each use the same instance of the variable. 因此,使用标准
var
发生的是多个闭包或循环都使用变量的相同实例。
Unless you want the var
behavior, always use let
or const
. 除非您想要
var
行为,否则请始终使用let
或const
。
Put a newline after the label compatibility_loop
在
compatibility_loop
循环标签后放置换行符
The first function is pushing into activeParts
. 第一个功能是推入
activeParts
。 The second function is looping activeParts
. 第二个功能是循环
activeParts
。 This can go on forever, or longer than expected. 这可以永远持续下去,或者比预期的时间更长。 Pushing into the array could possibly make the loop limit never reached.
推入阵列可能会使循环极限永远不会达到。 Put a log on the length of
activeParts
in the second function to see if it is growing out of control. 在第二个函数中的
activeParts
长度上放置一个日志,以查看它是否不受控制。
Your code should be OK if varifiedKeypoints.length
has reasonable value. 如果
varifiedKeypoints.length
具有合理的值,则您的代码应该可以。 And all internal variables are declared properly! 并且所有内部变量都正确声明!
You have two loops (this inner can start at j=i+1
to save time and multiple calculations) with few conditions inside. 您有两个循环(内部可以从
j=i+1
以节省时间和进行多次计算),并且内部条件很少。
function compareKeypoints(varifiedKeypoints) {
outer_loop: for (let i = 0; i < varifiedKeypoints.length; i++) {
let initialKeypoint = varifiedKeypoints[i];
for (let j = i+1; j < varifiedKeypoints.length; j++) {
let comparisonKeypoint = varifiedKeypoints[j];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.