Execute 3 tasks with setInterval

1

I have 3 querys and I need every time the setInterval triggers it to execute a query at the end, go back to the first one.

 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);
    
asked by anonymous 01.05.2018 / 07:58

1 answer

1

You can create an array with the 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);
    
01.05.2018 / 08:15