Using a recursive function, I implemented the code to log the tree structure into console.log. To introduce delays during processing, I included setTimeout in the code. However, adding setTimeout resulted in a different output order during processing, with varying delay times.
The purpose of this code is:
Upon finding a child node after searching for the first category title, the code recursively searches for the second category title and then inquires about the children value. This process continues for subsequent categories. Adding setTimeout causes all categories to be processed one after the other rather than simultaneously.
What could be causing this unexpected behavior?
var time = 0;
function searchTree(v, t){
$(v).each(function(i,k){
setTimeout(function(){
if (t == 'clone'){
console.log(k.sCategoryTitle);
if (k.children){
searchTree(k.children,'clone');
}
}
}, time = time + 100);
});
}
View sample code: http://jsfiddle.net/uahg5qd9/3/