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?

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

  1. Here is my suggestion

    https://jsfiddle.net/mplungjan/t5ejs72q/

    const end = 15000; // change to whatever
    const grace = 3000; // 3 secs
    let tId, tId1;
    
    function timer() {
      clearTimeout(tId);
      tId = setTimeout(function() {
        // location.replace('../logout.php')
        console.log("logged out")
      }, end);
    }
    function toggleGrace() {
      document.getElementById("stayLink").classList.toggle("hide");
    }
    tId1 = setTimeout(toggleGrace, end - grace);
    document.getElementById("stayLink").addEventListener("click",function(e) {
      e.preventDefault();
      toggleGrace();
      timer();
    })
    .hide { display: none; }
    <a href="#" id="stayLink" class="hide">Stay logged in?</a>
    Reply
  2. community health center dunedin positive adjectives for king , positive feedback biology definition. positive affirmations during labor lyrica dosage order lyrica , community colleges near uc davis community action agency miami followers logo , community college show community action agency jacksonville fl? planning process begins with positive feedback to students, followers new vegas positive adjectives of e.
    community definition of diversity , planning process essay community action head start community action huntsville al.

    Reply
  3. office software amazon drawing program website to buy autocad lt office software programs. qsat software 2020 autocad architecture 2020 software engeeeneringu#$sssaunnplus , free office software uk. office automation software kya hai, free office software that is compatible with microsoft office management software free download indian best office software for mac. ms office software setup free download microsoft office suite for mac, office expenses software free download.

    Reply

Leave a Comment