如何停止递归和/或使用更新的变量重新启动当前函数

发布于 2024-10-08 05:55:50 字数 1388 浏览 1 评论 0 原文

我有两个数组,它们根据标准进行自我更新和彼此更新(描述的时间比我怀疑的解决方案要长得多)。

我最终得到的是一个在 while 循环中调用自身的函数。正如你可以想象的,这会导致大量的递归。

这是一个示例(保持简短)

var buildArray=firstFunction(new Array(), existingArray)

function firstFunction(thisArray, existingArray){
     for(test1=0; test1<existingArray.length; test1++){
         if(existingArray[test1][3]=='2'){
           secondFunction(thisArray, existingArray, test1);
        }
     }

function secondFunction(thisArray, existingArray, t1){
       for(test2=0; test2<thisArray.length; test2++){
          if(thisArray[test1]<=existingArray[test2][1] || thisArray[test1]>existingArray[test2[0]){
            // do a bunch of stuff to existingArray, now that existingArray has changed, the whole process needs to start again FROM THE BEGINNING!!!
     return firstFunction(new Array(), existingArray);

              // check that the value isn't already in the 'thisArray'
   var check= new Array(existingArray[test1]);
  else if (jQuery.inArray(check, thisArray==-1){
         // value isn't in the new array, so add it
        thisArray.push(check);
       // thisArray has changed. need to restart the the second function
       secondFunction(thisArray,existingArray);
     }

   }
 }

}
}

我希望

return secondFunction(thisArray, existingArray);

能够重置并重新启动该功能,但显然这没有发生。

有没有办法停止当前的函数和循环并使用更新的变量重新启动?

I've got two arrays which are updating themselves and each other based on criteria (it is way longer to describe than I suspect the solution is).

What I end up with is a function which calls itself within a while loop. As you can imagine, this causes a ridiculous amount of recursion.

Here's an example (keeping it short)

var buildArray=firstFunction(new Array(), existingArray)

function firstFunction(thisArray, existingArray){
     for(test1=0; test1<existingArray.length; test1++){
         if(existingArray[test1][3]=='2'){
           secondFunction(thisArray, existingArray, test1);
        }
     }

function secondFunction(thisArray, existingArray, t1){
       for(test2=0; test2<thisArray.length; test2++){
          if(thisArray[test1]<=existingArray[test2][1] || thisArray[test1]>existingArray[test2[0]){
            // do a bunch of stuff to existingArray, now that existingArray has changed, the whole process needs to start again FROM THE BEGINNING!!!
     return firstFunction(new Array(), existingArray);

              // check that the value isn't already in the 'thisArray'
   var check= new Array(existingArray[test1]);
  else if (jQuery.inArray(check, thisArray==-1){
         // value isn't in the new array, so add it
        thisArray.push(check);
       // thisArray has changed. need to restart the the second function
       secondFunction(thisArray,existingArray);
     }

   }
 }

}
}

I was hoping that

return secondFunction(thisArray, existingArray);

would reset and restart the function, but apparently that isn't happening.

Is there a way to stop the current function and loops and restart with the updated variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无畏 2024-10-15 05:55:50

我不明白你正在尝试做什么,但是基于 return 停止第二个函数中的执行这一事实,并且 thisArray 永远不会改变,
您可以向第一个函数添加一个循环:

function firstFunction(thisArray, existingArray){
    var restart = true;
    while(restart)
    {
        restart = false;
         for(test1=0; !restart && test1<existingArray.length; test1++){
             if(existingArray[test1][3]=='2'){
               if(secondFunction(thisArray, existingArray, test1))
               {
                restart = true;
               }
            }
         }
    }

并在第二个函数中添加一个循环,而不是返回数组返回 true:

  if(thisArray[test1]<=existingArray[test2][1] || thisArray[test1]>existingArray[test2[0]){
    // do a bunch of stuff to existingArray, now that existingArray has changed, the whole process needs to start again FROM THE BEGINNING!!!
 return true;

i do not get what you are trying yo do, however based on the fact that return stop the execution in the secondFunction, and thisArray is never changed,
you can add a loop to the firstFunction:

function firstFunction(thisArray, existingArray){
    var restart = true;
    while(restart)
    {
        restart = false;
         for(test1=0; !restart && test1<existingArray.length; test1++){
             if(existingArray[test1][3]=='2'){
               if(secondFunction(thisArray, existingArray, test1))
               {
                restart = true;
               }
            }
         }
    }

and in the secondFunction instead of returning the array return true:

  if(thisArray[test1]<=existingArray[test2][1] || thisArray[test1]>existingArray[test2[0]){
    // do a bunch of stuff to existingArray, now that existingArray has changed, the whole process needs to start again FROM THE BEGINNING!!!
 return true;
赠意 2024-10-15 05:55:50

我注意到的一些事情:

  • secondFunction() 中,即使您有参数 t1,您也可以
  • 在第二次调用 secondFunction() 时 访问 test1 不包含最后一个参数,
  • 第二次调用 secondFunction() 之前没有 return,即使您在 OP 中提到了它,
  • 还有一行 else if ( jQuery.inArray(check, thisArray==-1){,缺少一个“)”
  • 有一行 if(thisArray[test1]<=existingArray[test2][1] || thisArray [test1]>existingArray[test2[0]){
  • for(test1=0; test1 中缺少一个“]”,它应该是 for(var test1... 让解析器知道它在
  • for(test2=0; test2 它应该是 for(var test2... 让解析器知道它在递归中是一个独立的变量(当然假设你希望这种情况发生)
  • ...

我是说您编辑了代码以将其发布在这里。这当然很好,但这让我相信代码的某些部分丢失了。

就缺少的 var 而言,这可能符合您的兴趣,但解析器可能会感到困惑。如果没有 var ,则变量是在文档级别定义的,而不是在函数级别或最内部范围定义。我不知道你是否意识到这一点或者这是否是一个问题。

Some things I noticed:

  • in secondFunction() you access test1 even though you have a parameter t1
  • the second call to secondFunction() doesn't contain that last parameter
  • the second call to secondFunction() doesn't have a return before it even though you mentioned it in your OP
  • there's a line else if (jQuery.inArray(check, thisArray==-1){, which is missing a ")"
  • there's a line if(thisArray[test1]<=existingArray[test2][1] || thisArray[test1]>existingArray[test2[0]){ missing a "]"
  • in for(test1=0; test1<existingArray.length; test1++){ it should be for(var test1... to let the parser know it's a variable on its own in the recursion (assuming you want this to happen of course)
  • in for(test2=0; test2<thisArray.length; test2++){ it should be for(var test2... to let the parser know it's a variable on its own in the recursion (assuming you want this to happen of course)
  • ...

I'm saying that you edited the code to post it here. That is fine of course, but it makes me believe that some part of the code is missing.

As far as the missing vars is concerned, that may be in your interest, but the parser may get confused. If there's no var the variable is defined at document-level, not at function-level nor at the innermost scope. I don't know if you aware of this or if it's an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文