我有以下会话超时jquery语句:
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script type="text/javascript">
idleTime = 0;
$(document).ready(function () {
// Inkrementerer every minute
var idleInterval = setInterval(timerIncrement, 1000); // 1 minute
// reset the counter when mouse moved or key pressed
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
document.getElementById("<%= lblSessionTime.ClientID %>").innerText = idleTime;
idleTime = idleTime + 1;
if (idleTime > 19) { // 20 minutes
// Send status to Database
}
}
</script>
当空闲时间超过20分钟后,我想向MySQL数据库发送状态,该页面现在处于离线状态。我无法弄清楚如何使用JQuery。有任何想法吗?
更新2:
所有aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Drivhus 1.aspx.cs" Inherits="FS07.Drivhus_1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script type="text/javascript">
idleTime = 0;
$(document).ready(function () {
// Inkrementerer inaktiv tilstand counteren hvert minut.
var idleInterval = setInterval(timerIncrement, 1000); // 1 minute
// Nulstiller counteren når musen er bevæget eller der bliver tastet.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
document.getElementById("<%= lblSessionTime.ClientID %>").innerText = idleTime;
idleTime = idleTime + 1;
if (idleTime > 19) { // 20 minutes
// Send status to Database
PageMethods.SendStatustoDB();
alert("Connected");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
<h6>Session time left: <asp:Label ID="lblSessionTime" runat="server"></asp:Label> minutes.</h6>
<h2>Her vises SCADA GUI</h2>
</div>
</form>
</body>
</html>
为什么不调用C#WebMethod?这将是一种AJAX方法,将需要使用ScriptManager。
ASPX:确保在FORM标记内添加ScriptManager对象并进行设置EnablePageMethods = true
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
/* REST OF YOUR HTML */
</form>
JavaScript:您将需要修改您的timerIncrement函数并调用C#WebMethod
function timerIncrement() {
document.getElementById("<%= lblSessionTime.ClientID %>").innerText = idleTime;
idleTime = idleTime + 1;
if (idleTime > 19) { // 20 minutes
// Call C# WebMethod to send status to Database
PageMethods.SendStatustoDB();
}
}
C#代码隐藏:您需要参考以下内容:
using System.Web.Services;
然后添加以下方法,并确保将其放在[WebMethod]
方法声明之前:
[WebMethod]
public static void SendStatustoDB()
{
// code to connect to DB and set status!
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句