Close Session Automatically

1

I need to place a timer inside this sweet alert message but where would I have to place it and how? here I have the option to continue navigating in the location.reload () function; but in the isConfirm that a timer of 30 seconds will appear where the session is automatically closed.

  var timeout; 
    document.onmousemove = function(){ 
        clearTimeout(timeout); 
        timeout = setTimeout(function(){
            swal({
              title: "Tu sesión ha expirado",
              text: '',
              type: "warning",
              showCancelButton: true,
              cancelButtonText: "Continuar Trabajando",
              confirmButtonClass: "btn-danger",
              confirmButtonText: "Salir",
              closeOnConfirm: false,
              showLoaderOnConfirm: true,
            },
            function(isConfirm){
                if (isConfirm) {
                    $.ajax({
                        url: base_url + "auth/logout",
                        type: "POST",
                        success:function(resp){
                            window.location.href= base_url;
                        }

                    });
                }else{
                    location.reload();
                }

            });
        }, 60000); 
    } 

    
asked by WilsonicX 13.03.2018 в 18:32
source

1 answer

0

You can place it in $(document).ready() so that in each action that reloads the DOM your setTimeout will restart, since you make sure that the function does not run until the page is ready to execute the code javascript

$(document).ready(function () {
    //funcion
}

I do not know how flexible your idea is, I suggest you use jquery-confirm and I'll give you this example of its operation, you can place your AJAX code so that it points to the function of your controller that kills the session and redirects to another view.

$( document ).ready(function() {
    $.confirm({
       title: 'Alerta!',
        content: 'La sesión esta por expirar.',
        autoClose: 'logoutUser|10000',
        buttons: {
            logoutUser: {
                text: 'cerrar sesión',
                action: function () {
                    $.alert('La sesión ha expirado');
                    //tu codigo AJAX                    
                }
            },
            cancelar: function () {
                $.alert('cancelado');
            }
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.js"></script>
    
answered by 13.03.2018 / 18:51
source