I'm currently working on implementing a feature that tracks user activity by recording check-ins and checkouts before their session expires. I have set up a session timeout to handle this but now I need to call a method using JavaScript and Ajax to run just before the session ends. However, when trying to execute the method, it isn't working as expected. Below is the code snippet:
Admin.Master.cs
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (!this.IsPostBack)
{
Session["Reset"] = true;
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
Page.ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
}
}
[WebMethod]
public void logout_timeout() //This is the method I am attempting to run
{
}
Admin.Master
<script type="text/javascript">
function SessionExpireAlert(timeout)
{
debugger
var seconds = timeout / 1000;
document.getElementsByName("secondsIdle").innerHTML = seconds;
document.getElementsByName("seconds").innerHTML = seconds;
setInterval(function () {
seconds--;
document.getElementById("seconds").innerHTML = seconds;
document.getElementById("secondsIdle").innerHTML = seconds;
}, 1000);
setTimeout(function ()
{
//Show Popup before 20 seconds of timeout.
$.ajax({
type: "POST",
url: "Admin.Master/logout_timeout",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: OnSuccess,
error: OnError
});
$find("mpeTimeout").show();
}, timeout - 20 * 1000);
setTimeout(function () {
window.location = "Login.aspx";
// Additional AJAX request example
//$.ajax(
// {
// type: "POST",
// contentType: "application/json; charset=utf-8",
// url: "Case.aspx/removeTrans",
// data: "{ 'DelData':'" + transId + "'}",
// success: function (result) { window.location.reload(true); console.log("successful!"); }
// })
}, timeout);
};