'html / javascript Reading text file and updating content only if value has changed
I am using a small javascript to update a text on a website by reading the content from a text file on the server. While the code itself does what I expect, I'd like to check whether the contents have changed, instead of overwriting it every 20 seconds (which restarts the marquee of course).
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("kuechempd").innerHTML =
"<marquee scrolldelay=50 scrollamount=2 width=140px>"+this.responseText+"</marquee>";
}
};
xhttp.open("GET", "/sensorvalues/kueche_mpd.txt", true);
xhttp.send();
I am a real noob with javascript, typically I would use a temporary variable to compare the contents like
if old <> new then
old=new
do_update
fi
But i dont know whether thats possible with javascript
Thank you in advance for any hints ;-)
Solution 1:[1]
You may do like this:
Initially, save the content to localstorage
Then, check it to the new content coming from api. Here, string_a -> OLD, string_b -> NEW
string_a.localeCompare(string_b);
Expected Return:
0: exact match
-1: string_a < string_b
1: string_a > string_b
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tahsin_siad |
