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();
});
}
It didn’t work, I updated the question with the way I tried to do.
– Wendler
It even worked, loaded the function, but did not obey the rules, was not updating according to the status.
– Wendler
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.
– Wendler
I put everything in one function, see: https://jsfiddle.net/48owsy99/4/
– Sam
Ixi, it’s not working no. : / I updated the question with a more complete part of the updated functionLeilao()
– Wendler
You want to see the status of multiple items with Ajax or expect to receive only one?
– Sam
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.
– Wendler
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/
– Sam