Functions with ajax make loop for stop running

Asked

Viewed 45 times

0

I have a function that executes when I click a button. When clicking, the function should execute a loop for and if the value of that position of the for correspond to any of the ifs then it executes the function. These functions are all called ajax.

var checkbox_tipos = document.forms[1];

document.getElementById('gerar_dss').addEventListener('click', function(event){

    event.preventDefault();

    test : for (i = 0; i < checkbox_tipos.length; i++) {
        console.log(i);
        console.log(checkbox_tipos.length);

        if(checkbox_tipos[i].checked){
            console.log(checkbox_tipos[i].value);
            if(checkbox_tipos[i].value === 'dss_chuva'){
                console.log('entrou em chuva');
                dss_chuva();
                continue test;
            }
            if(checkbox_tipos[i].value === 'dss_cota'){
                console.log('entrou em cota');
                dss_cota();
                continue test;
            }
            if(checkbox_tipos[i].value === 'dss_vazao'){
                console.log('entrou em vazao');
                dss_vazao();
                continue test;
            }
        }
    }
});

function dss_vazao(){

    for (i = 0; i < checkbox_estacoes.length; i++) {
        if (checkbox_estacoes[i].checked) {
          estacao[i]=checkbox_estacoes[i].value;
        }
    }

    var ajax = new XMLHttpRequest();

    ajax.onreadystatechange = function() {
        if (ajax.readyState == 4 && ajax.status == 200) {
            var data = JSON.parse(ajax.responseText);

            console.log(data.result);
            alert(data.result);
        }
    }

    ajax.open('POST', '../server/gerar-vazoes.php', true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    ajax.send('estacoes='+estacao+'&data_inicio='+data_inicio.value+'&data_fim='+data_fim.value+ 
        '&nome_arquivo='+nome_arquivo.value+'&observacao='+observacao.value+'&tipo='+tipo.value);
}

Doing tests, I realized that when performing one of the functions the for stops rotating and thus does not travel continues with the loop. Looking around I saw the statemt "continue" and tried to apply but did not work. The problem is: how to make it continue? I thank you already!

[EDIT 1]: For better understanding: the problem is that when, in the for, enters one of the conditions if and performs one of the for does not continue the loop. Example: the for should rotate 3 times, but on the first round of the loop when entering one of the ifHe doesn’t continue the two laps that he should do, as if the for was closed.

  • I didn’t fully understand your problem, but some time ago I had problems with several requests being made within a for. That was my question: How to wait until all requisitions are complete?

  • @Marconi thanks. I made an Edit, see if you can understand better.

  • @Ramones is still not very clear :/ but could you pass the full code on github or something and describe the steps to understand the problem ? And about these requests Ajax strongly recommend using Jquery instead of Xmlhttprequest directly.

1 answer

0


I solved the problem by identifying that in for there was the var variable serving as counter. When inserting the var worked as expected.

Browser other questions tagged

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