在asp.net应用程序中我有一个Jquery函数。
我需要检查会话是否有这样的超时
function Myfunction() {
if(FuctionHasTimedOut)
{
return 1;
}
else
{
return 2;
}
}
怎么做?非常感谢任何帮助
答案 0 :(得分:0)
如果意味着服务器端会话状态,那么你可以在这样的函数中对服务器执行ajax请求:
function Myfunction() {
$.post('{urlToHandler}', function(){
// server should return some session value, that would indicate that session state is accesible
});
}
答案 1 :(得分:0)
试试这个插件http://philpalmieri.com/js_sandbox/timedLogout/jquery-idleTimeout.js
/** Run with defaults **/
$(document).ready(function(){
$(document).idleTimeout();
});
/** With Optional Overrides **/
$(document).ready(function(){
$(document).idleTimeout({
inactivity: 30000,
noconfirm: 10000,
sessionAlive: 10000
});
});
答案 2 :(得分:0)
我会为此使用PageMethod
。这是我找到的一些代码here:
[WebMethod]
public static bool HasSessionTimedOut()
{
HttpSessionState session = HttpContext.Current.Session;
// I put this value into Session at the beginning.
DateTime? sessionStart = session[SessionKeys.SessionStart] as DateTime?;
bool isTimeout = false;
if (!sessionStart.HasValue)
{
isTimeout = true;
}
else
{
TimeSpan elapsed = DateTime.Now - sessionStart.Value;
isTimeout = elapsed.TotalMinutes > session.Timeout;
}
return isTimeout;
}
这是我的Javascript:
<script type="text/javascript">
$(function() {
var callback = function(isTimeout) {
if (isTimeout) {
// Show your pop-up here...
}
};
setInterval(
function() {
// Don't forget to set EnablePageMethods="true" on your ScriptManager.
PageMethods.HasSessionTimedOut(false, callback);
},
30000
);
});
</script>