Hello! I can't figure out how to save the data received from the server, then use it to compare it with new data. Here is my code sending a request to the server. I would be grateful for your ideas.

document.querySelector('button').addEventListener('click', function(e){ var lanes = document.querySelector('select.lanes'); var laneNumber = lanes.options[lanes.options.selectedIndex].value; clearInterval(interval); interval = setInterval(function(){ var request = new XMLHttpRequest(); request.open("GET", "http://server.com" + laneNumber); request.send(); var prevThrow; request.addEventListener('readystatechange', function () { if (request.readyState === request.DONE) { var throws = JSON.parse(request.responseText); prevThrow = throws.slice(0); var trTrows = document.querySelector('.throws'); var trScore = document.querySelector('.score'); for (var i = 0; i < throws.length; i++){ var td = document.createElement('td'); td.setAttribute('colspan', '2'); td.dataset.index = i; var oneThrow = throws[i]; td.textContent = oneThrow; trScore.appendChild(td); } } }); }, 5000); }, false); 
  • where do you want to compare and with what? - Grundy
  • I want to compare the data received from the server at the moment with the data that came to me from the server in the previous answer. They change. Changes I need to use in the future. - Julia Petrishina
  • take out the var prevThrow from setInterval Then it will be saved between calls - Grundy

2 answers 2

Use a variable outside the function:

 var GLOBAL_INFO = {}; document.querySelector('button').addEventListener('click', function(e){ var lanes = document.querySelector('select.lanes'); var laneNumber = lanes.options[lanes.options.selectedIndex].value; clearInterval(interval); interval = setInterval(function(){ var request = new XMLHttpRequest(); request.open("GET", "http://server.com" + laneNumber); request.send(); var prevThrow; request.addEventListener('readystatechange', function () { if (request.readyState === request.DONE) { var throws = JSON.parse(request.responseText); prevThrow = throws.slice(0); GLOBAL_INFO.throws = throws; var trTrows = document.querySelector('.throws'); var trScore = document.querySelector('.score'); for (var i = 0; i < throws.length; i++){...} } }); }, 5000); }, false); ... var myThrows = GLOBAL_INFO.throws; if( myThrows ){ YOU_CODE } 
  • Use a global variable in Javascript, where closures are the essence of the language? - Vladimir Morulus
  • Not correctly expressed, declare a variable outside the function. Thanks, corrected. - borodatych
  • And if the dialogue between the client and the server is not through ajax, but in the usual way? Where then to store global variables? Without consideration of web storage, the topic has not been disclosed - Sergey
  • The task is to compare between calls. When there is a task in preservation between sessions, then we can talk about repositories. About the difference between localStorage and sessionStorage , and how it all works on mobile devices, if the task touches it. You can always put thoughts in the next answer, it is not prohibited. - borodatych

You can declare a variable to hold the previous response in the scope of the function responsible for sending the request before setInterval.

 (function() { var lastResponse = null; setInterval(function(){ someRequest({ response: function(responseText) { if (responseText!==lastResponse) { // Do something } lastResponse = responseText; } }); }, 5000); })(); 

Also, depending on whether you need an exact comparison or perpert, you can store the previous value as JSON text or as an object, but then you have to use a function like extend or clone to avoid sending a copy of the object by reference.

  • Calling a person to use the construct (function(){})(); , explain what it means, and soon questions will start to appear, why it does not work for me. - borodatych