【发布时间】:2015-06-03 11:55:06
【问题描述】:
在我的函数中,我正在遍历一棵树,以查找节点中的特定属性。该函数被递归调用,并在找到属性或树中没有更多节点时停止。
但是,当我运行该函数时,它进入了我认为是无限循环的状态,Firefox 冻结,我必须停止该过程。然后我在函数中添加了setTimeout,以确定是什么导致了问题,但现在整个事情运行正常。如果有人对问题所在有一些了解,我将在此处发布该功能(顺便说一句,我正在使用 AngularJS 并取消选中复选框):
$scope.uncheckNode = function(nodeId, subitem){
// Loop through each node in the sub nodes
for (i = 0; i<subitem.length; i++){
// If the node is found, uncheck it and break from the loop
if (subitem[i].Id == nodeId){
subitem[i].selected = false;
break;
}
// Otherwise get the sub nodes of the subnodes
// (an empty array if undefined)
// Check if it has any nodes and continue with the recursion
else{
var subsubitem = subitem[i].Subitem || [];
if (subsubitem.length > 0){
$scope.uncheckNode(nodeId, subsubitem);
}
}
}
}
【问题讨论】:
-
如果在子项中找不到
nodeId,它将无限循环。
标签: javascript angularjs infinite-loop