Forum Moderators: open

Message Too Old, No Replies

window.setTimeout with confirmation message function

         

fototex

6:10 pm on Jan 16, 2010 (gmt 0)

10+ Year Member



Hi friends,

I am trying to use the window.setTimeout feature so that a message pop-up with yes/no appears on the screen asking whether to extend the session.

*If yes is clicked, the current page reloads.
*If no is clicked, nothing happens. (the session will expire anyway).

Could not figure out the code.
Sorry if it had been post before.

rocknbil

8:26 pm on Jan 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



confirm returns true or false, if OK is clicked, true, otherwise false. When testing for true or false, you don't even need an '==true', just

if (condition) {
// true
}

So

function reloadPage(url) {
var ok = confirm('Extend session?');
if (ok) { document.location=url; }
return false;
}

should do it. Put that in a document with this to test.

<p><a href="some-file.html" onclick="return reloadPage(this.href);">test</a></p>

Drag_Racer

5:47 am on Jan 18, 2010 (gmt 0)

10+ Year Member



why not be fancy...

<a href="some-file.html" onclick="return confirm('Extend session?');">test</a>

fototex

7:01 pm on Jan 18, 2010 (gmt 0)

10+ Year Member



I have used the following code:

window.setTimeout("if (confirm('continue session?')){window.reload}",5000) ;

The code pops up the confirmation message but after confirming the message, nothing happens. The page does not reload itself.

Any advises appreciated.

Fotiman

7:36 pm on Jan 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@fototex, you have the setTimeout in the wrong place (note also that you don't need the 'window' portion):

if (confirm('continue session?')) {
setTimeout(window.reload, 5000);
}

if you wanted this to appear sometime after the page loaded:


setTimeout(function () {
if (confirm('continue session?')) {
setTimeout(window.reload, 5000);
}
}, 1000 * 60 * 9);

That will give you the confirm dialog 9 minutes after the page loads, and if the user clicks Yes, will reload the page 5 seconds later.

Also note, avoid using the quoted string version of setTimeout. That is, instead of this:

setTimeout("somefunction()", 1000);

pass in a function reference instead:

setTimeout(somefunction, 1000);