Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:43:16+00:00 2026-05-30T20:43:16+00:00

I’m working on a auto logout using setTimeout and clearTimeout. Should be really straight

  • 0

I’m working on a auto logout using setTimeout and clearTimeout. Should be really straight forward but the clear timeout doesn’t seem to stop the timeout and the function is called anyway. I have it set up that the session variable populates a text input that the javascript then reads and compares to the time now. if the difference is more than or equal to 20 seconds an overlay div shows giving an option to renew the session or it will logout in 10 seconds. The script that check the last activity auto runs every ten seconds. Obviously once I’ve got it working the timings will be longer – these are just for testing purposes.

I have trawled the web but can’t seem to find an obvious solution as it not so complicated and looks like it really should just work!

Here’s my code:

var t = 0;
function check_status(){
    var time_now = Math.floor(new Date().getTime() / 1000);
    var last_activity = document.getElementById("last_activity").value;
    var since_last_activity = time_now-last_activity;
    console.info(since_last_activity);

    if(since_last_activity >= 20){
        // show div
        document.getElementById("logout_warning").style.height = document.documentElement.clientHeight+"px";
        document.getElementById("logout_warning").style.display = 'block';

        // start countdown
        var t = setTimeout("logout();", 10000);
    }
}

function logout(){
    document.getElementById("logout_warning").style.display = 'none';
    location.href="/user/logout";
}

function renew(){
    clearTimeout(t);

    var time_now = Math.floor(new Date().getTime() / 1000);
    document.getElementById("last_activity").value = time_now;
    document.getElementById("logout_warning").style.display = 'none';
}

setInterval('check_status()', 10000);

and then in the body…

<div id="logout_warning" style="display:none; width:100%; height:500px; top:0px; left:0px; position:absolute; background-image:url('/images/overlay.png'); z-index:100000;">
    <div style="width:300px; position:relative; margin:200px auto; border:1px solid #000000; background-color:#FFFFFF; padding:10px; text-align:center;">
        You're going to be logged out in 10 seconds! oh no!<br/><br/>
        <button type="button" onclick="renew();">Click here</button> to renew your session
    </div>
</div>

The last activity variable is pulled from a text input that is populated by a session variable. This bit seems to work, as in when you click the renew button the value of the input is changed and the console output shows that the time since last activity has been reset.

I hope this makes sense. Please ask for clarification but I’m really stumped on this one!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:43:18+00:00Added an answer on May 30, 2026 at 8:43 pm

    You have two ts.

    var t = 0;
    

    This one in the outside scope.

    var t = setTimeout("logout();", 10000);
    

    This one in the inside scope.

    clearTimeout(t);
    

    This is reading the one on the outside scope.

    Remove the var from the one on the inside scope.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.