Copy to clipboard with delay

3

I use this code to copy the clipboard:

<script type="text/javascript">

    function copiarAlPortapapeles("p1") {         
      var aux = document.createElement("input");
      aux.setAttribute("value", document.getElementById("p1").innerHTML);
      document.body.appendChild(aux);
      aux.select();
      document.execCommand("copy");
      document.body.removeChild(aux);
    }

</script>

To execute it use: onclick="copiarAlPortapapeles()"

The code works but I have a problem, this function is in submit , which shows a result by ajax.

What happens is that before copying to the clipboard the ajax function appears (in php) so it copies the previous one.

I thought about delaying the code so that: onclick="setTimeout ('copiarAlPortapapeles();', 2000);"

But it does not work for me ... any help?

the ajax code:

<script language="javascript">// <![CDATA[
$(document).ready(function() {
   // Esta primera parte crea un loader no es necesaria
    $().ajaxStart(function() {
        $('#loading').show();
        $('#result').hide();
    }).ajaxStop(function() {
        $('#loading').hide();
        $('#result').fadeIn('slow');
    });
   // Interceptamos el evento submit
    $('#form, #fat, #signup-forma').submit(function() {

  // Enviamos el formulario usando AJAX
        $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),
            // Mostramos un mensaje con la respuesta de PHP
            success: function(data) {
                $('#result').html(data);

            }
        })        
        return false;
    }); 
})
// ]]></script>
    
asked by Enrique Ballester Gómez 30.08.2016 в 10:00
source

1 answer

4

Call it from the function success of the call $.ajax()

...  
success: function(data) {
    $('#result').html(data);
    copiarAlPortapapeles();
}
...
    
answered by 30.08.2016 / 14:19
source