0

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 ;-)

Sebastian Heyn
  • 387
  • 3
  • 15
  • Are you talking about the text file content? – Joy Dey May 20 '22 at 09:38
  • yes. so one time you read the text file and it contains "aaaa" and the next time it is read it still reads "aaaa" - I dont want the script to overwrite the element since it would reset the marquee. – Sebastian Heyn May 20 '22 at 09:42

1 Answers1

1

You may do like this:

  1. Initially, save the content to localstorage

  2. 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

LocalCompare

Original answer

tahsin_siad
  • 86
  • 1
  • 11