Function within Jquery what is the execution order?

Asked

Viewed 229 times

4

I have a function in jquery that has a while that writes data to the database, and after this while it prints on console that the data was saved, the function is like this:

function gravaDados(){
var qtde_linhas = 6 //ele pega a quantidade de linhas da tabela a ser salvo
var x = 1;
while( x <= qtde_linhas ) {

    ...//dados AJAX
    ... console.log('Dados gravados'); //salva os registros

x++;
}
console.log('dados salvos com sucesso '+x);
}

In the console appears that were saved the data and the current position of x is 7 ie he saved the 6 lines in the database, but my doubt is the following because the console.log from within the while is last printed?

ele percorreu o while mas apresentou o utlimo console antes

As seen in the image it records the data but prints the console in reverse, this is due to the delay of writing the data in the database, or if I insert another function in place of the last console I may have problems of the data not being finished and execute the other function? For now it’s working, but I have this question, or is there a way for him to check if the while is finished to go with the structure?

3 answers

4


This is probably because the console that’s inside the while is also within a callback ajax, which is asynchronous.

In other words, the code fires the ajax requests to store in the bank, exits the while and runs the console that is following the while. Later (even if only milliseconds) the bank’s answers start to arrive and will fire their console.log.

In these cases it is even possible for the console.log inside while to arrive in a different order, that is to say not in the exact sequence with which they were "fired".

  • 1

    Thank you very much for clearing me this doubt had already read some documentation but nothing that said with the clarity you answered, I believe that this answer will help other people.

  • @Wagnerfernandomomesso I’m glad I was clear and helpful.

3

Because the execution of ajax codes by default is asynchronous, ie jquery will not wait for the end of the execution of its routine to save the data in the database before giving the console.log of "Data saved successfully".

I imagine that’s your problem.

Here’s a little more detailed exploration. http://www.diogomatheus.com.br/blog/php/requisicoes-sincronas-e-assincronas/

  • Good explanation of the link informed, thank you

2

Wagner Fernando, another thing that was not informed, is that the function first can not have "-" (trace). It can be like this for example:

gravaDados() 

And the <= also can not have space < =

function gravaDados(){
    var qtde_linhas = 6 //ele pega a quantidade de linhas da tabela a ser salvo
    var x = 1;
    while ( x <= qtde_linhas ) {
        console.log('Dados gravados'); //salva os registros
        x++;
    }
    console.log('dados salvos com sucesso '+x);
}
gravaDados();

Another thing is I don’t know why it was printed last there, but here it was in normal order, with the code above:

inserir a descrição da imagem aqui

  • the code I put in the question was just to exemplify my problem, but inside my while there is an ajax that saves the data that by the answers of Sérgio and Léo Nogueira is the reason the impression have some time to appear.

  • You can then explain this in your question there, editing. It is clearer for people who also have this doubt :D

Browser other questions tagged

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