I have been extending the current sample clock synchronization application to add some more features to show more capabilities of jQuery. In the last update I added functionality to display latency numbers when service was called every X number of minutes. If you will let the page running for quite some time you will see how quickly the entries get appended to the display. What we really want is that latest entries should be displayed at the top. So basically instead of appending the new entries, we want to prepend the new entry to the last entry added. We are in luck here. jQuery does offer couple of methods here that makes it easy. prepend, prependTo, before, insertBefore methods are your friend here. It turns out that all these methods get mapped to one function behind the scene, insertBefore. Following code snippet from the application shows entries are added in reverse order.
var elLastLatencyEntry;
var elLastMessageEntry;
function displayLatency() {
var elToAppend = null;
if (latency == -1) { elToAppend = $("<p>Initializing</p>"); }
else { elToAppend = $("<p>" + latency + " ms</p>"); }
if (null == elLastLatencyEntry)
{ elLastLatencyEntry = elToAppend.appendTo(elLatencyDisplay); }
else { elLastLatencyEntry = elToAppend.prependTo(elLastLatencyEntry); }
}
You can see from the code that I save the last entry that was added in a variable for later use. This avoids over head of finding the latest entry again and again every X number of seconds.
Insert new HTML elements in reverse order using jQuery
0x1F is an invalid start of a value
Learn Python: How to ignore SSL verification for HTTP requests
How to host your web site from your home
Automate testing of web page element transition with Selenium
Alert and Confirm pop up using BootBox in AngularJS
AngularJS Grouped Bar Chart and Line Chart using D3
How to lock and unlock account in Asp.Net Identity provider
2022 © Byteblocks, ALL Rights Reserved. Privacy Policy | Terms of Use