Update feather function once while loading

0

I have the following script:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
async function update() {
  for($i=0;$i<300;$i++){ // a cada 300x (5min) faz um refresh
      atualizaLeilao();
      await sleep(1000);
  }
 location.reload(); 
}

$(document).ready(function () {
    update();
});
The updateLeilao () function returns a status that is defined as: leilao_status

There are statuses 1, 2 and 3. Knowing the statuses I did the following:

if (leilao_status == 3) {

**AQUI A FUNÇÃO NOVA.**

}else{

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
async function update() {
  for($i=0;$i<300;$i++){ // a cada 300x (5min) faz um refresh
      atualizaLeilao();
      await sleep(1000);
  }
 location.reload(); 
}

$(document).ready(function () {
    update();
});

}

In the case of status = 3, I need it to run the updateLeilao () function only once, that is, when the page loads or refreshes.

The problem is that to get the status you need to run the updateLeilao () function before, without executing the function before you can not know the status.

How can I get it to fetch the status before running the functions according to the rules?

I tried this way:

function atualizaLeilao(){
    var leiloes = '';
    $.ajaxSetup({ cache: false });
    $('.itemLeiao').each(function () {
        var auctionId = $(this).attr('title');
        if (leiloes != '') leiloes = leiloes + ',';
        leiloes = leiloes + auctionId;
    });
    if(leiloes){
        $.ajax({
            url: 'get.php',
            dataType: 'json',
            type: 'POST',
            data: 'leiloes=' + leiloes,
            global: false,
            success: function (data) {
                $.each(data, function (j, item) {
                    var leilao_id = item.leilao.id_leilao;
                    var leilao_status = item.leilao.status;

            });
            },
        });        
  }
 }
    document.addEventListener("DOMContentLoaded", function() {
    atualizaLeilao();
    });



    if (leilao_status == 3) {

    $(document).ready(function () {
    setInterval('atualizaLeilao()', 300);
    atualizaLeilao();
    });

    } else {

    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }
    async function update() {
      for($i=0;$i<300;$i++){ // a cada 300x (5min) faz um refresh
          atualizaLeilao();
          await sleep(1000);
      }
     location.reload(); 
    }

    $(document).ready(function () {
        update();
    });

    }
    
asked by anonymous 28.02.2018 / 22:36

2 answers

0

try to run the updateLeilao function as soon as the program loads, so do something like:

(function()){
    atualizaLeilao();
}());

This will run the function without it having to be called, so it should return a status.

    
28.02.2018 / 23:11
0

Make a return leilao_status; in the function atualizaLeilao() , something like this:

function atualizaLeilao(){
   leilao_status = 3;
   return leilao_status;
}

And in if , you compare whether the function return is 3 :

if (atualizaLeilao() == 3) {

**AQUI A FUNÇÃO NOVA.**

}else{

...

}

Edit

But for the returned value of the function to be compared repeatedly, you can include the code in a function. This% "loose"% will only make the comparison 1 time when loading the page. So the code would look like this:

function atualizaLeilao(){
    var leilao_status = item.leilao.status;
    return leilao_status;
}

function checa(){
   if (atualizaLeilao() == 3) {

      var tempo = setInterval('checa()', 300);

   }else{

      clearInterval(tempo);

      function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
      }
      async function update() {
        for($i=0;$i<300;$i++){ // a cada 300x (5min) faz um refresh
            atualizaLeilao();
            await sleep(1000);
        }
       location.reload(); 
      }

       update();

   }
}

$(document).ready(function(){
   checa();
});
    
28.02.2018 / 22:53