1

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 don't need anything complex like jQuery. Just some nice clean OOP JS handmade will be fine.

3 Answers 3

2

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;
};
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

-1
<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
  }
} 

6 Comments

'setTimeout("confirmStay()", 60000)' shouldn't that just be "confirmStay" for the 1st argument?
I think both versions (string and a function reference) are supported.
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.
Both string and function reference are supported, however string version uses eval to parse the string so it's theoretically less secure.
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).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.