Recursive Functions - ff6347/extendscript GitHub Wiki
Recursive functions are functions that call themselves. Beware! This can lead to infinite loops. You have to make sure that there is a point where the script stops or your computer will crash. This is based on that.
// This Object needs to be global
var meta = new Object();
meta.res = "";
main();
function main() {
var val = 11;
var cnt = 0;
var result = recurs(val,cnt); // call the function
alert(meta.res); // this is already the end
return "End Of function 'main()'";// just a return type
};
/**
* thie recursive function that cals itself
* @param {Number} p1 Gets reduced every call
* @param {Number} counter will count up every call
* @return {null} just stops fills up a global Object
*/
function recurs(p1,counter) {
p1--;
counter++;
// this is important if you don't check we will run into an infinite loop
if (p1 <= 0) {
$.writeln(counter);// write to console. Extendscript specific call
meta.res = String("We are at the end of the recursion in " + counter +" loops"); // set the global
return ;// end it
}else {
// THIS IS RECURSIVE the function calls itself!
recurs(p1,counter);
}
}