How can I log out the user even if they don't respond to JavaScript's "confirm()"?

This was the initial code I was using:

setInterval(function(){
  r=confirm("You are about to be logged out! Press cancel if you want to remain logged in.");
  if (r == true){
    window.location.href = '../logout.php';
  }else{
    location.reload();
  }
},30000);

The confirm dialog awaits action from the user. If the action is "cancel", they remain logged in. If the action is "OK" the are redirected to the logout.php page. The issue is that if the user does not respond, they are not logged out after those elapsed 30 seconds.

Then I thought I may use two time intervals:

setInterval(function(){
  window.location.href = '../logout.php';
},60000);


setInterval(function(){
  r=confirm("You are about to be logged out! Press cancel if you want to remain logged in.");
  if (r == true){
    window.location.href = '../logout.php';
  }else{
    location.reload();
  }
},30000);

but since the confirm() method halts the script, the 60000 ms is never realised. Is there a way I can get this to work?

5 thoughts on “How can I log out the user even if they don't respond to JavaScript's "confirm()"?”

Leave a Comment