Update of Random Divs

Asked

Viewed 31 times

0

In this my Script below it is doing an update of a Divs pulling a file tv2.php of so much and I try time... until then it works, the more I need it Intercale between two files.

Example
1 and 1 minute change content by switching tv1.php file to tv2.php in a loop

As I have no knowledge of javascript that would a judinha of how I can correct this code for this

  <script>
  $(function () {
  function getContadorMapa() {
    $.get("tv2.php", function (result) {
        $('#latestData').html(result);
    });
  }

 getContadorMapa();
 setInterval(getContadorMapa, 20000);
 });

 </script>

1 answer

1


A simple use of an IF can solve

Note I think the use of setInterval so it can conflict, maybe it’s better to use setTimeout, to know the difference between the two recommend you see the following answers I have asked in other questions:

$(function () {
    var minutos = 1; //Um minuto

    var url1 = "tv1.php";
    var url2 = "tv2.php";

    var urlAtual = url1; //Coloque a url inicial

    function getContadorMapa() {
        $.get(urlAtual, function (result) {
            $('#latestData').html(result);
        }).always(function() {
            //Quando a requisição termina troca dispara always

            //Se a url atual for igua a url 1 então troca pra 2, caso contrário troca pra 1
            urlAtual = urlAtual === url1 ? url2 : url1;

            setTimeout(getContadorMapa, 60 * 1000 * minutos);
        });
    }

    getContadorMapa();
});

If you need to implement more urls in the future use an array:

$(function () {
    var minutos = 1; //Um minuto

    var urls = [
                   "tv1.php",
                   "tv2.php",
                   "tvA.php",
                   "tvX.php",
                   "tvY.php"
               ];

    var i = 0; //Começa no item zero da array que no caso é o "tv1.php"
    var total = urls.length; //Pega o total de itens

    function getContadorMapa() {
        $.get(urls[i], function (result) {
            $('#latestData').html(result);
        }).always(function() {
            //Quando a requisição termina troca dispara always

            //Soma mais um a variável e assim passa para o proximo item do array
            i++;

            //Se i for maior que o tamanho do array então volta pra zero
            if (!(i < total)) {
                 i = 0;
            }

            setTimeout(getContadorMapa, 60 * 1000 * minutos);
        });
    }

    getContadorMapa();
});

Browser other questions tagged

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