Update feathers function once when loading

Asked

Viewed 41 times

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 function updateLeilao() returns a status that is set to: auction_status

There are status 1, 2 and 3. Knowing the status 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 case for status = 3, I need it to perform the function updateLeilao() only once, ie when the page is loaded or when it is updated.

The problem is that to get the status you need to perform the function updateLeilao() before, without performing the function before has no way to know the status.

How can I make it fetch the status before then perform the functions according to the rules?


I tried that 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();
    });

    }

2 answers

0

Makes a return leilao_status; in function atualizaLeilao(), something like that:

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

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

if (atualizaLeilao() == 3) {

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

}else{

...

}

Edit

But in order for the value returned from the function to be repeatedly compared, you can include the code in a function. This if "loose" will only make the comparison 1 time on page loading. Then 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();
});
  • It didn’t work, I updated the question with the way I tried to do.

  • It even worked, loaded the function, but did not obey the rules, was not updating according to the status.

  • If you have status 2 or 1 it can change at any time, but if you already click with status 3 it will not change.

  • I put everything in one function, see: https://jsfiddle.net/48owsy99/4/

  • Ixi, it’s not working no. : / I updated the question with a more complete part of the updated functionLeilao()

  • You want to see the status of multiple items with Ajax or expect to receive only one?

  • When you access an item on a page, it loads this JS with the data from that item. Each item can have a different status. In addition to the status it picks up other information too, which I did not put there.

  • I updated the Fiddle. Because it is the following: when calling the function that returns the Ajax, the return is not immediate, then you have to use a callback .then... See code: https://jsfiddle.net/48owsy99/8/

Show 3 more comments

0

try to perform the function updateLeilao just when the program loads, do something like:

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

With this the program will execute the function without it needing to be called, so it should return a status.

  • It didn’t work. I updated the question with a form I tried, almost worked.

  • Loaded the function, but did not obey the rules, was not updating according to the status

  • oh, vc can not set a fixed variable within the function, when creating a var leilao_status vc makes the variable exist only while the function is executed, create it from the outside (no value) and within the function remove the var, so only update its value

  • Um, I tried to take out the var but it didn’t work either, I created it within the function, unsuccessfully.

Browser other questions tagged

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