As the user pans around a Google Map, a list of of currently visible markers is updated. This list contains up to 1000 items, and it slows down when several hundred li's are shown or hidden all at once. It's less than half a second, but it's getting annoying.
An array (newLiList) contains the items which should now be visible. Another array (currentLiList) has the items which were previously visible. Both arrays contain the ids of the li's as indexes.
for (var i in newLiList) {
if (currentLiList[i] != true) {
$("ul#theList li#"+i).show();
}
}
for (var i in currentLiList) {
if (newLiList[i] != true) {
$("ul#theList li#"+i).hide();
}
}
Is there a faster way to do this?
Style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document. getElementById("element").
let list = document. getElementById( "myList" ); Step 3: Now iterate all the array items using JavaScript forEach and at each iteration, create a li element and put the innerText value the same as the current item, and append the li at the list.
In JavaScript, we can hide the elements using the style. display or by using the style. visibility. The visibility property in JavaScript is also used to hide an element.
For every single .show() and .hide() function call, you're causing a redraw in the browser.
Setting the whole list to display none before you change the list items will prevent the browser from having to recalculate the flow of the document every time, and then you could set the list back to block.
Alternatively, if you don't want the flicker of the list disappearing, you could clone the list, make all the items hide that should hide, and then replace the old list with the new.
You could store the <li>
elements in an array directly so you don't have to do hundreds of CSS selector lookups. Then instead of $("ul#theList li#"+i)
you just do liArray[i]
.
FYI, "ul#theList li#<i>"
is equivalent to just "#<i>"
since element IDs are (supposed to be) unique. You don't need the extra context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With