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