I have this object tree which is represented as follows (simplified):
var root = [
{ name: 'child1',
data: 'other_data',
children: [ {
name: 'grand_child1',
data: 'other_data',
children: [...]
}, ... ]
},
{ name: 'child2',
data: 'other_data',
children: [ {
name: 'grand_child2',
data: 'other_data',
children: [...]
}, ... ]
}
]
I need to loop through this object tree and instantiate objects based on the data provided in the children, but this process takes a few seconds and it freezes the browser in the mean time.
I need to have a timeout between iterations of my loop, to give the browser a chance to clear the event queue.
How would I go about this?
This is how I am currently looping through them, without the timeout. I am currently using a mutual recursion pattern.
function performSingleNodeComputation(node) {
performHeavyComputation(node.data)
if(node.children) {
performMultipleNodeComputation(node.children);
}
}
function performMultipleNodeComputation(nodes) {
nodes.forEach(function (node) {
performSingleNodeComputation(node);
});
}
performMultipleNodeComputation(root);
To reiterate, I need a timeout between every invocation of performHeavyComputation