Javascript function calling another javascript function inside a while

Asked

Viewed 6,548 times

2

I’m having a problem executing a javascript function that in its scope has a while that according to how the counter is incremented, 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 inside while does not. Can someone help me?

  • 1

    One hour to variable cont is numerical, another is string, This obviously won’t work. What do you want to do? Give relevant information to solve the problem, if possible put a functional example. How do you make sure that the function is not executed? How are the elements in HTML?

  • Function B is a verification function, the user enters information in the field and it returns if this information is registered in the database. If true, it blocks the button next to the field to add the information to the BD, if false, it leaves the button free to include the data in the BD. In case the functionA makes the inclusion of the data in the BD and runs the function again so that when the pop-up window is closed the input button is blocked.

  • Wouldn’t it be better to do while (cont > 0), else cont = 0;? than converting from numeric to string.

  • From what I understand, your function is ok, in case you want to check fields 1_campo, 2_campo ..., Specify the problem better.

  • In the case while is executed until it has been verified how many fields there are in the document. The fields are named as follows: 1_field, 2_field... They can be added or removed according to the amount of information to be saved at once, so cont gets 'p', that is until there are fields it will call function B, when it calls Document.getElementById(cont+'_field') and equals null cont takes p and ends.

  • You’re talking... Function B is a verification function, the user enters information in the field and it returns if this information is registered in the database, you go into a database to check this? why don’t you just do everything on the server side?

  • Yes, she takes what is typed in the field and searches the database if that information is already registered

  • It can be a lot of stuff, and it seems that the problem is in function B. You can post her code?

  • The problem is not in function B, because I run it to check the entered data and it works normally, the problem is when it is called by function A. If executed only once, it works, but from the second interaction on.

Show 4 more comments

2 answers

0

run the function here and worked normally, already checked if the document fields are with the correct id (1_field, 2_field)? maybe then it would be better because it would avoid the conversion of the variable type....

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

  • In parts it worked, but only when the data is inserted in the last field that the button of the previous fields are blocked

0

Nathan, I believe you may be encountering some problem with the scope of the variables, between trying to make the following changes.

try to use some more structured way to group your elemethos, whether using a namespace, one class, one data-custom

Since 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 selector [campo|='namespace'], will take all elements whose value of the selected field (in the case id) possess the namespace informed, that is to say start with a given string follow of a hifen, for this in the above example input#campos is not captured by the selector.

how are you passing your cont to the funcaoB, I believe you’re redoing the consultation (getElementById), then I advise you to pass the 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 example above, even making use of the window.setTimeout to simulate a request AJAX, I had no problem getting the id and the type of each input.

in any case it becomes difficult to help you without seeing the whole script, after all your problem is possibly ma funcaoB

Browser other questions tagged

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