I've looked on several questions, but none seem to solve my problem. The project needs to open a list of emojis inside a div (modal). So when the users press the emoji button, a modal with emojis will appear. If we list hundreds of emojis, then this is almost instantly done. If we put thousands of emojis, then it takes several seconds to load the modal.
I've provided a snippet with a demo array of emojis. Tried classic for approach and also createDocumentFragment (inspired from here), both of them having the same effect.
Looking for a solution that can render emojis one at a time, like in a queue. I know that I can push the DOM element in the queue and when it is loaded I can shift to the next element. The only thing is that I do not know when content.appendChild(button) or fragment.appendChild(button) is displayed. I've read that you can use setTimeout function, but I do not know what time to set or if it is recommended in this case.
Thank you!
function showEmojis() {
let symbols = [
"☺", "☻", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"" , "", "", "", "", "", "", "", "", "", "", "", "", "",
"☹", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "☠", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "☕", "", "", "", "", "", "", "", ""]
// Just to a have big array with emojis
for (var i = 0; i < 6; i++) {
symbols = symbols.concat(symbols)
}
let content = document.getElementById("content")
// Simple for
/*
for(var i=0; i < symbols.length; i++) {
let unicode = symbols[i]
let button = document.createElement("button")
button.classList.add("emoji-button")
button.innerHTML = unicode
button.onclick = function() {
console.log("selected")
}
content.appendChild(button)
}*/
// Document fragment
var fragment = document.createDocumentFragment()
for (var i = 0; i < symbols.length; i++) {
let unicode = symbols[i]
let button = document.createElement("button")
button.classList.add("emoji-button")
button.innerHTML = unicode
button.onclick = function() {
console.log("selected")
}
fragment.appendChild(button)
}
content.appendChild(fragment)
}
.emoji-show-button {
font-size: 15px;
}
.emoji-button {
width: 28px;
height: 28px;
padding: 0px;
margin: 5px;
background: white;
border-style: none;
font-size: 20px;
}
<button class="emoji-show-button" onclick="showEmojis()">Show emojis</button>
<div id="content"></div>