Perform 3 tasks with setInterval

Asked

Viewed 69 times

1

I have 3 querys and I need that every time setInterval trigger it execute a query at the end return to the first.

 setInterval(function () {

 var query1 = "select * from weather.forecast where woeid = '429100' and u = 'c'";
 var query2 = "select * from weather.forecast where woeid = '455823' and u = 'c'";
 var query3 = "select * from weather.forecast where woeid = '456964' and u = 'c'";

 var queryURL = "https://query.yahooapis.com/v1/public/yql?q="+query2+"&lang=ptBR&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys/";

 $.getJSON(queryURL, function (data) {

 var results = data.query.results;
 var firstResult = results.channel;
 console.log(firstResult);
 var location = firstResult.location.city;
 var temperaturaHoje = firstResult.item.condition.temp;
 var condicaoHoje = firstResult.item.condition.code;

 });

 },5000);

1 answer

1

You can create an array with queries and increment a counter.

var c = 0;

setInterval(function () {

 var queries = ["select * from weather.forecast where woeid = '429100' and u = 'c'", "select * from weather.forecast where woeid = '455823' and u = 'c'", "select * from weather.forecast where woeid = '456964' and u = 'c'"]

 var queryURL = "https://query.yahooapis.com/v1/public/yql?q="+queries[c]+"&lang=ptBR&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys/";

 $.getJSON(queryURL, function (data) {
   document.write(results);
   var results = data.query.results;
   var firstResult = results.channel;
   console.log(firstResult);
   var location = firstResult.location.city;
   var temperaturaHoje = firstResult.item.condition.temp;
   var condicaoHoje = firstResult.item.condition.code;
   c += 1;
   if (c > 2) {
     c = 0;
   }
 });

 },5000);
  • it even works but of the problem of CORS. strange that I put without the setInterval it not of the problem of Cors.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.