javascript function that calls another javascript function within a while [closed]

1

I have a problem executing a javascript function that in its scope has a while that according to the counter is incremented, it is called a second javascript function passing as parameter the counter value:

function funcaoA(){
    var cont = 1;
    while (cont != 'p'){
        if (document.getElementById(cont+'_campo') != null){
            funcaoB(cont, 'tipo_do_campo');
            cont++;
        } else {
            cont = 'p';
        }
        alert('mensagem');
    }
}

When executing this code, the requested message is executed but the B () function within the while does not execute. Can anyone help me?

    
asked by anonymous 01.02.2016 / 11:10

2 answers

0

I ran the function here and it worked fine, have you checked that the document fields have the correct id (1_field, 2_field)? maybe this would be better because it would avoid the conversion of the type of the variable ....

function funcaoB(a, b) {
  alert("faz alguma coisa com " + a + " e " + b)
}

function funcaoA() {
  var cont = 1;
  while (document.getElementById(cont + '_campo')) {
    funcaoB(cont, 'tipo_do_campo');
    cont++;
    alert('mensagem');
  }
}
    
01.02.2016 / 11:44
0

Nathan, I believe you should be encountering some problem with the scope of the variables, try to make the following changes.

try to use some more structured way to group your elements, either using namespace , class , data-custom

As your code already uses something similar to a namespace, then I'll post an example using namespace:

var campos = document.querySelectorAll("[id|='campo']");

[].forEach.call(campos, function (campo, indice) {
  console.log(campo, indice);
});
<input id="campo-1" type="text" />
<input id="campo-2" type="text" />
<input id="campo-3" type="text" />
<input id="campo-4" type="text" />
<input id="campo-5" type="text" />
<input id="campo-6" type="text" />
<input id="campos" type="text" />

The [campo|='namespace'] selector will take all elements whose value of the selected field (in the id case) has namespace informed, that is, start with a certain string , followed by hifen so in the example above the input#campos is not captured by the selector.

As you are passing your cont to funcaoB , I believe you are redoing the query ( getElementById ), so I advise you to pass input#campo-n .

var campos = document.querySelectorAll("[id|='campo']");
var funcaoB = function (campo, indice) {
  window.setTimeout(function () {
    console.log(campo.id, campo.type);
  }, (indice + 1) * 1000)
};

[].forEach.call(campos, function (campo, indice) {
  funcaoB(campo, indice);
});
<input id="campo-1" type="text" />
<input id="campo-2" type="number" />
<input id="campo-3" type="date" />
<input id="campo-4" type="time" />
<input id="campo-5" type="email" />
<input id="campo-6" type="tel" />
<input id="campos" type="text" />

Note that in the above example, even using window.setTimeout to simulate a AJAX request, I had no problem catching id and type of every input .

In any case it is difficult to help you without seeing the whole script, after all your problem is possibly less than%     

01.02.2016 / 13:15