I am tying to dynamically append divs to a DOM element using pure javascript. When I am using it without the setTimeout function it works properly, but when I am using setTimeout function the document fragment isn't appended (I can see it exists if I console.log it but I am obviously doing something wrong).
I have to use a setTimeout since the array that I am about to loop through consists of 1-5000 items and I don't want to lock the entire gui while rendering those.
<html>
<body>
<head>
</head>
<div id="outputContainerFlowchart"></div>
<script>
console.log(document.getElementById("outputContainerFlowchart"));
let dummyArr = ["D", "u", "M"];
let cont = document.getElementById("outputContainerFlowchart");
let frag = document.createDocumentFragment();
for (const letter of Object.values(dummyArr)) {
setTimeout(function() {
var printedNodeTest = document.createElement("div");
printedNodeTest.className = "printedNode";
printedNodeTest.innerHTML = "Testnode";
frag.appendChild(printedNodeTest);
}, 0);
}
cont.appendChild(frag);
</script>
</body>
</html>
Edit:
I realized that I'd forgot that setTimeout "makes it async" (which James also noticed, thanks!) - no wonder it didn't work. Wrapped it inside a Promise instead and this is the result:
<html>
<body>
<head>
</head>
<div id="outputContainerFlowchart"></div>
<script>
console.log("initializing");
console.log(document.getElementById("outputContainerFlowchart"));
let dummyArr = ["D", "u", "M"];
let cont = document.getElementById("outputContainerFlowchart");
let renderFunc = function(dummyArr) {
return new Promise(function(resolve, reject) {
let frag = document.createDocumentFragment();
for (const letter of Object.values(dummyArr)) {
console.log("current letter inside for: "+letter);
console.log("inside timeout");
var printedNodeTest = document.createElement("div");
printedNodeTest.className = "printedNode";
printedNodeTest.innerHTML = "Testnode";
frag.appendChild(printedNodeTest);
}
resolve(frag);
});
}
renderFunc(dummyArr).then(function(frag) {
console.log("appending frag");
cont.appendChild(frag);
});
</script>
</body>
</html>