te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript clearTimeout not clearing timeout!.. this shouldn't be hard? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript clearTimeout not clearing timeout!.. this shouldn't be hard? - Stack Overflow

programmeradmin4浏览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 pares 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 plicated 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!

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 pares 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 plicated 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!

Share Improve this question edited Oct 23, 2018 at 5:37 GROVER. 4,3782 gold badges31 silver badges75 bronze badges asked Feb 27, 2012 at 9:14 Helen Danger BurnsHelen Danger Burns 4412 gold badges11 silver badges29 bronze badges 1
  • Don't pass strings for evaluation to setTimeout, pass functions. setTimeout(logout, 10000); – Quentin Commented Feb 27, 2012 at 9:16
Add a ment  | 

3 Answers 3

Reset to default 11

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.

That's because you're redefining the variable in the function and it bees local to that function, the global t is still 0:

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

remove the var before the variable!

You're overshadowing the global variable t with a local one:

var t = 0;
function check_status() {
   var t = setTimeout("logout();", 10000);
// ^^^^
}

function renew() {
    clearTimeout(t);
}

Remove the var to make t refer to the global variable.

发布评论

评论列表(0)

  1. 暂无评论