I have a basic understanding of setTimeout and why it is necessary to use immediately invoked functions to do what it says in my questions title. My attempt doesn't accomplish what I'm trying to do, however. Self.instruct sets a reactive data element so that the message should be displayed to the screen. Only the last elements of each object-array end up being displayed.
export default {
name: 'hello',
props: ['tolearn'],
data () {
return {
message: 'Welcome to Your Vue.js App',
toLearn: {},
instruct: ''
}
},
methods: {
showSlow: function(){
var self = this;
categoriesRef.once('value', function(snapshot){
var val = snapshot.val();
var sysName = val.name;
var sysChildren = val.children;
console.log(Object.keys(val.children) + " are the aspect names")
sysChildren.forEach(function(aspect){
(function(aspect){
//self.instruct = aspect.name + ' is one aspect of ' + sysName + '.';
setTimeout(function(){
aspect.children.forEach(function(group){
(function(group){
//self.instruct = aspect.name + ' contains the group ' + group.name + '.';
setTimeout(function(){
group.children.forEach(function(item){
(function(item){
console.log(item.name + ' is being considered')
self.instruct = group.name + ' contains the item ' + item.name + '.';
setTimeout(function(){
self.instruct = '';
}.bind(self), 800)
})(item)
})
}.bind(self), 100)
})(group)
})
}.bind(self), 1500)
})(aspect)
})
})
}
}
}
basically, val taken from the snapshot is a json tree structure 3 levels deep, each level containing a name and a children property.
The console.log outputs several 'itemname is being considered' messages at once when the program is finished running.
Can anyone help me figure out how to write this correctly so that all of the tree's elements are displayed? Somehow my IIFE's aren't enough.
.bind(self)calls are redundant as you don't accessthisin your callbacks anywaydata.instructbecause it gets a pointer to the Vue object to use as 'this' (I've added the rest of the code to the question so you can see). I originally had it without the bind calls, and only added them after seeing this answer, which made the code work stackoverflow.com/questions/37465289/…