Update content only when accessing the div

0

I have a step-by-step form of which with each step saved in the information database. In the last step I bring the information saved as follows with jquery:

<script type="text/javascript">
function mostrar(){   
    $(document).ready(function(){                      
            $.ajax({               
                    type:'post', 
                    dataType: 'json',
                    url: 'atualizar.php',
                    success: function(dados){

                       for(var i=0;dados.length>i;i++){
                           $('#listar').append(dados[0]);
                           $('#listar').append(dados[i].id+'</td><td>'+dados[i].nome+'</td><td>'+dados[i].email+'</td></tr>');
                       }

                    console.log(dados);    
                    }
            });
    });   
}
setInterval(mostrar, 2000);
</script>

It is working perfectly, but in this case I am updating the div for 2 seconds and would like to know if it is possible to update only when accessing the div in the last step. The HTML looks like this:

<fieldset>
    <!-- Última etapa do step-by-step -->
    <h4>Confirme seus dados:</h4>

    <div id="listar"></div>

    <form role="form" action="" method="post" id="contact-form">
    <div class="f1-buttons">
        <button type="button" class="btn btn-previous">Alterar</button>
        <button type="button" class="btn btn-primary">Finalizar</button>
    </div>
    </form>
</fieldset>
    
asked by anonymous 15.12.2017 / 17:20

2 answers

1

You can know when you are in the last step by checking if there is a button with the text "Finish" in fieldset active. This check is done in the function below:

function scroll_to_class(element_class, removed_height) {
    var scroll_to = $(element_class).offset().top - removed_height;
    if($(window).scrollTop() != scroll_to) {
        $('html, body').stop().animate({scrollTop: scroll_to}, 0);
    }
   if( $('fieldset:visible').find('button:contains("Finalizar")').length == 1){
      // última etapa. Faça o que deseja aqui
      alert("Última etapa");
   }
}
    
15.12.2017 / 18:16
0

Ideally, you should create a function that identifies when you are on a form with the focus option.

When editing, do not upgrade.

And as for saving the data, I imagine it is better to save in session or cookie and only fill in the DB at the end.

This will prevent bank memory wear from bringing you performance, always think about it.

    
15.12.2017 / 17:52