也许我过度思考这个问题,但我需要做的是看看从开始会话时间(1分钟)减去当前时间的时间差。
<script type="text/javascript">
var mySessionTimer;
@functions {
public int PopupShowDelay
{
get {
DateTime currentSetTimeout = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout);
DateTime currentServerTime = DateTime.Now;
TimeSpan duration = (currentServerTime.Subtract(currentSetTimeout));
return 60000 * (int)duration.TotalMinutes;
}
}
}
function callJSSessionTimer() {
var sessionTimeoutWarning = @PopupShowDelay;
var sTimeout = parseInt(sessionTimeoutWarning);
mySessionTimer = setTimeout('SessionEnd()', sTimeout);
}
function SessionEnd() {
clearTimeout(mySessionTimer);
window.location = "/Account/sessionover";
}
@if (userInfo != null)
{
if (userInfo.chosenAMT == "True")
{
@:callJSSessionTimer();
} else
{
@:clearTimeout(mySessionTimer);
}
} else {
@:clearTimeout(mySessionTimer);
}
</script>
因此持续时间的值为 -00:01:00 ,这在技术上是正确的,因为 currentSetTimeout 1分钟< / strong>它获取今天的日期/时间,因为它从 currentSetTimeout 中减去它后面的一分钟。
因此,当用户从一个页面跳到另一个页面时,所有这一切都是为了跟踪剩余会话时间。目前,当用户转到另一个页面时,它会重置时间并且不准确。
我怎么能按照我需要的方式去做呢?
答案 0 :(得分:1)
当用户在会话期间转到另一个页面时,您可以使用html5会话存储来维护该值:
if (sessionStorage.popupShowDelay) {
sessionStorage.popupShowDelay = Number(sessionStorage.clickcount);
} else {
DateTime currentSetTimeout = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout);
DateTime currentServerTime = DateTime.Now;
TimeSpan duration = (currentServerTime.Subtract(currentSetTimeout));
sessionStorage.popupShowDelay = 60000 * (int)duration.TotalMinutes;
}
您可以在此处查看更多信息: https://www.w3schools.com/html/html5_webstorage.asp
答案 1 :(得分:0)
从概念上讲,您的问题是当您从一页到另一页时,计数器会重置。所以你必须保持会话服务器端的开始时间。如果您使用的是ASP.NET,则可以使用会话变量来执行此操作。其他平台也有类似的东西。他们都使用cookie工作。祝你好运!
答案 2 :(得分:0)
知道了!
@functions {
public int PopupShowDelay
{
get {
if (Session["currentSetTimeout"] != null)
{
DateTime dt1 = DateTime.Parse(Session["currentSetTimeout"].ToString());
DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());
TimeSpan span = dt1 - dt2;
return (int)span.TotalMilliseconds;
}
return 0;
}
}
}