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?

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

  1. With havin so much content and articles do you ever run into
    any issues of plagorism or copyright infringement?
    My blog has a lot of completely unique content I’ve either
    written myself or outsourced but it looks like a lot of it is popping it up all
    over the internet without my permission. Do you know any ways to help stop content
    from being stolen? I’d certainly appreciate it. http://herreramedical.org/sildenafil

    Reply