tags:

views:

176

answers:

3

I need a script that i can put on any page in an application and it will run X amount of time. When it hits the amount of time that is specified in the js it will open either an overlay on the page or a child popup window. Inside either window i will give you what the text should say.

in the new window they will have two buttons.

resume logout

if they click resume you will kill the overlay or popup window and refresh the parent page. if they click logout you will redirect to a url.

all of this should be configured in the js. The overlay window will be simple so i dont need anything complex like jquery. just some nice clean OOP js handmade will be fine.

A: 
<body onload="timerStart()">

...

function timerStart(){
  setTimeout(confirmStay, 60000)   // 1 minute
}

function confirmStay() {
  if (confirm("Press OK to stay here.")) {
    // user pressed OK
    timerStart();   // restart timer
  } else {
    // user pressed Cancel
    location.href="http://example.com";  //redirect to homepage
  }
}
Zed
'setTimeout("confirmStay()", 60000)' shouldn't that just be "confirmStay" for the 1st argument?
alex
I think both versions (string and a function reference) are supported.
finpingvin
I have always put there a string containing a js statement... but I'm not catching up with all the new fashions, so it might be outdated.
Zed
Both string and function reference are supported, however string version uses eval to parse the string so it's theoretically less secure.
RaYell
Never use the string version, as it invokes "eval()" unnecessarily. Instead, pass a function reference or an anonymous function. So "setTimeout("confirmStay()", 60000)" becomes "setTimeout(confirmStay, 60000);" (also note the semi-colon—every statement should end with one).
Steve Harrison
Thanks guys for the comments, I have changed it to function reference. Will also try to remember this next time I use setTimeout :)
Zed
A: 

I'm assuming that this has something to do with refreshing a session to preserve a login. If that is the case then you should keep in mind that the user may not respond to your dialog for some time after it pops up. Meaning, your popup may come up after 10 minutes and your session may last for 15 minutes, but if it takes longer than 5 minutes for the user to respond then the session will expire and the refresh of the page will not help you.

<body onload="setTimeout(refreshLogin, 600000)">
    <script>
        function refreshLogin() {
          if (confirm("Press OK to refresh this page and renew your login.")) {
            window.location.reload();   // refresh the page
          } else {
            window.location.href="http://example.com"  //redirect to somewhere
          }
        }
    </script>
</body>
Prestaul
+2  A: 

Yet another approach:

Usage:

var timer = reloadTimer({  seconds:5, logoutURL: '/logout',
                           message:'Do you want to stay logged in?'});
    timer.start();

Implementation:

var reloadTimer = function (options) {
  var seconds = options.seconds || 0,
      logoutURL = options.logoutURL,
      message = options.message;

  this.start = function () {
    setTimeout(function (){
      if ( confirm(message) ) {
        window.location.reload(true);
      } else {
        window.location.href = logoutURL;
      }
    }, seconds * 1000);
  }
  return this;
};
CMS