简体   繁体   English

使用递归JavaScript函数进行同步的利弊

[英]Pros and cons of using recursive JavaScript functions for synchronization

For synchronization (to serialize execution) I often use recursive functions in javascripts. 为了同步(序列化执行),我经常在javascript中使用递归函数。

For an example to serialize the execution of following code, 例如,要序列化以下代码的执行,

var array = [1,2,3,4,5,6,7,8,9];
//total delay 1s (1s for all)
for (var i=0; i<array.length; i++){
    (function(i){
         setTimeout(function(){console.log(array[i])},1000);
    })(i);
}

I use recursive code like this, 我使用这样的递归代码,

var array = [1,2,3,4,5,6,7,8,9];
//total delay 10s (1s for each)
var i = 0;
function fn(){
    setTimeout(function(){console.log(array[i])
        i++;
        if (i<array.length){
            fn();
        }
    },1000);

}
if (array.length > 0){
    fn();
}

Even though in many programming languages, recursive functions have issues with stack overflow, I don't see that drawback here unless we don't use return statements. 即使在许多编程语言中,递归函数都存在堆栈溢出问题,除非我们不使用return语句,否则我在这里看不到这种缺点。

My question is, What are the pros and cons of using recursive functions for synchronization in javascripts? 我的问题是,在JavaScript中使用递归函数进行同步有何利弊?

Real recursion involves functions that call themselves directly or indirectly. 真正的递归涉及直接或间接调用自身的函数。 This is a recursive function: 这是一个递归函数:

function factorial(n) {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}

Your code, however, merely arranges for the system to (sometimes) call your function again some time in the future . 但是,您的代码只是安排系统在将来的某个时候 (有时)再次调用您的函数。 By the time that function call happens, the original function call will have finished. 到函数调用发生时,原始函数调用将完成。 (It finishes basically immediately after setting up the timeout.) Thus, at any given time, there's only one call to the function in progress, so there won't be any issues with running out of stack space. (它基本上是在设置超时后立即完成的。)因此,在任何给定时间,只有一个调用正在进行中,因此堆栈空间用完不会有任何问题。

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

相关问题 使用javascript完全生成html的优点和缺点 - Pros and cons of fully generating html using javascript 在我们的表单中使用javascript有什么优缺点? - What are the pros and cons using javascript in our form? 使用tsc使用tsc命令将TypeScript转换为JavaScript的利弊 - Pros and Cons for using tsc to transpile TypeScript to JavaScript using tsc command 使用javascript函数的三种不同方式,但我不知道它的优缺点。 有人可以解释这些差异吗? - Three different ways of using javascript functions, but I don't know the pros and cons for it. Could someone explain the differences? Javascript中继承建模的优缺点? - Pros and cons of inheritance modeling in Javascript? 使用JavaScript添加<script>和<link>元素有哪些优缺点? - What are the pros and cons of adding <script> and <link> elements using JavaScript? 服务器端javascript实现的优缺点? - pros and cons of serverside javascript implementation? 用Java创建类的脚本的优缺点? - Pros and Cons of Class Creation Script in Javascript? Javascript类与对象之间的优缺点? - Javascript classes vs objects, pros and cons? Javascript对象定义技术,优缺点 - Javascript object definition techniques, pros and cons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM