D
D
dimkaholodov2017-10-24 19:43:59
JavaScript
dimkaholodov, 2017-10-24 19:43:59

How to properly store current data?

Good afternoon. Please help me figure it out, I do the following:
Every 5 seconds I get the data of the usd/rub exchange rate.

setInterval( function () {
$.getJSON( 'https://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.xchange+where+pair+=+%22USDRUB%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', function( data ) {
  var price = data.query.results.rate.Rate;
  console.log(price);
});
}, 5000 );


I need to use both the current value of the price variable
and the value received in 5 seconds, and then compare them.
Accordingly, I understand that price in my case is a local variable.
I tried to write a value to the property of the window object, but the values ​​are updated synchronously, as a result I got completely confused.
Can you please tell me if I can somehow use the value of the price variable in another function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Kostyukevich, 2017-10-24
@maxisoft

maybe like this

// глобальная переменная для курсов.
var data = [];

setInterval( function () {
$.getJSON( 'https://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.xchange+where+pair+=+%22USDRUB%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', function( data ) {
  var price = data.query.results.rate.Rate;
// проверяем что нужно тут

   // делаем кеш
   data = price; 

  console.log(price);
});
}, 5000 );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question