i have genrate a confirm message for session extend and it work properly. but i want that if user not respond to it than it will automatically hide.i have write following code .
if($("#takephotopageload").length > 0) {
        setInterval(function(){
          $.ajax({
            url: "check_session.php",
            //data: {action: 'clickme_clicked'},
            dataType: "json",
            success: function(data) {
              if(data.time > idletimeout) {
                var answer = confirm("Your session has expired. Would you like to extend it?");
                setInterval(function () { window.confirm.dialog("close"); }, 3000);
                if(answer == true) {
                  $.ajax({
                    url: "reactivate_session.php",
                    dataType: "json",
                    success: function() {},
                  });
                } else {
                  window.location.reload(true);
                }
              }
            }
          });
        },3000);
      }

Dani AI

Generated

Native browser dialogs (alert/confirm/prompt) are modal and blocking. You cannot programmatically close a native confirm() from script — browsers deliberately prevent that. So the approach you tried will not work. As suggested, use a non-blocking, custom dialog (jQuery UI, Bootstrap modal, or a tiny vanilla one) so you can start a countdown and auto-close it.

Below is a minimal, dependency‑free example you can drop into the page. It creates a simple overlay, shows a 10‑second countdown, calls your session-extend endpoint if the user clicks Extend, and treats timeout as a decline (reload/logout). Replace '/session/extend' with your server endpoint.

/* minimal session-warning: creates modal, auto-closes after 10s */
(function showSessionWarning(seconds, extendUrl){
  if(document.getElementById('sessionOverlay')) return;
  var html = '<div id="sessionOverlay" style="display:none;position:fixed;left:0;top:0;right:0;bottom:0;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,0.4);">' +
             '<div style="background:#fff;padding:18px;border-radius:4px;text-align:center;max-width:320px;">' +
             '<p>Your session will expire. Extend?</p>' +
             '<div style="margin:8px 0;"><span id="sessionCountdown"></span> seconds left</div>' +
             '<button id="sessionExtend">Extend</button> <button id="sessionDecline">Logout</button>' +
             '</div></div>';
  var div = document.createElement('div'); div.innerHTML = html;
  document.body.appendChild(div.firstChild);

  var overlay = document.getElementById('sessionOverlay'),
      cd = document.getElementById('sessionCountdown'),
      btnExt = document.getElementById('sessionExtend'),
      btnDecl = document.getElementById('sessionDecline'),
      t = seconds, tick;

  function open() {
    cd.textContent = t;
    overlay.style.display = 'flex';
    tick = setInterval(function(){
      t--; cd.textContent = t;
      if(t <= 0){ closeAndLogout(); }
    }, 1000);
  }
  function closeAndLogout(){ clearInterval(tick); overlay.style.display='none'; window.location.reload(true); }
  btnExt.addEventListener('click', function(){
    clearInterval(tick); overlay.style.display='none';
    fetch(extendUrl, { method:'POST', credentials:'same-origin' }).catch(()=>{});
  });
  btnDecl.addEventListener('click', closeAndLogout);

  // expose for manual use
  window.showSessionWarning = open;
})(10, '/session/extend');

Notes and tips:

  • Trigger showSessionWarning() after detecting idle/server response. Client-side idle detection (mousemove/keydown/touch) is often nicer than aggressive polling.
  • When the user extends, have the server update the session and return a simple 200/JSON. Keep the AJAX call lightweight.
  • For accessibility and polish, move focus into the dialog, support Esc key, and ensure the modal is read by screen readers.
  • If you prefer a ready UI, use a Bootstrap or jQuery UI modal — they let you programmatically hide it and are keyboard/accessibility friendly.

Recommended Answers

All 5 Replies

i have use this line of code for hide confirmation dialoge box setInterval(function () { window.confirm.dialog("close"); }, 3000);
but it not work.please give me some nedd ful suggestion

but it not work

What exactly does not work? Be specific.

Member Avatar for Member #120589

Is this PHP-related?

yes it is php related.in my case i have genrate a confirm window for user who is idle more than 1 minute by showing confirm window using
var answer = confirm("Your session has expired. Would you like to extend it?");
but i want to hide this confirm window within 10 second if user does not respond it.

Member Avatar for Member #120589

Not sure how you'd use php to close a js confirm. I'd suggest that you use a js solution. Possibly using a custom modal dialog (e.g. jQueryUI) coupled to a timer. Grabbing control of a browser's modals sounds difficult - not sure if this is possible - it would probably be a security risk if it were.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.