gpt4 book ai didi

javascript - 在 JavaScript 中获取和设置 session 变量

转载 作者:行者123 更新时间:2023-12-02 15:14:24 25 4
gpt4 key购买 nike

我在每个页面上都包含一个通用脚本,一旦用户登录,它就会将idletime变量初始化为0,并在每30秒后递增它,我为此编写了函数,该函数工作正常,但在递增之后变量我必须将该值设置为某个 session 级别变量,以便在每个页面刷新时此函数增量应该获得该增量值。请找到下面的代码

<script type="text/javascript">
var timeOut=600000;//This is timeout value in miliseconds
var idleTime = 0; // we shud get the incremented value on everypage refresh for this variable
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 30000); //30seconds
});

function timerIncrement() {
idleTime = idleTime + .5;//incrementing the counter by 30 seconds
var timeout= timeOut/60000;
if (idleTime > (timeout-2)) {
document.getElementById('logoutLink').click();
}
}
</script>

最佳答案

听起来像你想要的 web storage ,特别是 sessionStorage,其中有 excellent support (基本上,除了 Opera Mini 之外,甚至最近的所有东西都可以使用它 [甚至是 IE8])。

// On page load (note that it's a string or `undefined`):
var idleTime = parseFloat(sessionStorage.idleTime || "0");

// When updating it (it will automatically be converted to a string):
sessionStorage.idleTime = idleTime += .5;
<小时/>

话虽如此,如果您的目标是在 10 分钟不活动后单击注销链接,那么似乎可能会更简单一些:

$(document).ready(function() {
var lastActivity = parseInt(sessionStorage.lastActivity || "0") || Date.now();
setInterval(function() {
if (Date.now() - lastActivity > 600000) { // 600000 = 10 minutes in ms
document.getElementById('logoutLink').click();
}
}, 30000);

// In response to the user doing anything (I assume you're setting
// idleTime to 0 when the user does something
$(/*....*/).on(/*...*/, function() {
sessionStorage.lastActivity = lastActivity = Date.now();
});
});

关于javascript - 在 JavaScript 中获取和设置 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34587964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com