下面是我的 Javascript 函数。是否有可能让一个警报覆盖另一个警报。例如警报(消息);首先触发,但如果会话过期,则会触发警报(“会话已过期。您将被重定向到登录页面”);。您将看到第二个警报的唯一方法是单击警报(消息)上的确定。如果会话到期或以任何其他方式执行此操作,第二个警报是否可以仅覆盖第一个警报。
<!-- Session Timeout Function-->
<script language="javascript" type="text/javascript">
var sessionTimeout = <%= Session.Timeout %>;
var sessionTimeoutWarning = sessionTimeout - 1;
var timeOnPageLoad = new Date();
var warning = null;
var timeout = null;
if ( <% if (MasterPageTemplate.Classes.CmwSession.IsAuthenticated) Response.Write("1"); else Response.Write("0"); %> == 1)
{
//For warning
warning = setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);
//To redirect to the welcome page
timeout = setTimeout('RedirectToWelcomePage()',parseInt(sessionTimeout) * 60 * 1000);
}
//Session Warning
function SessionWarning() {
//minutes left for expiry
var minutesForExpiry = (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning));
var message = "Your session will expire in another " + minutesForExpiry +
" minutes! Please Save the data before the session expires";
alert(message);
var currentTime = new Date();
//time for expiry
var timeForExpiry = timeOnPageLoad.setMinutes(timeOnPageLoad.getMinutes() + parseInt(sessionTimeout));
//Current time is greater than the expiry time
if(Date.parse(currentTime) > timeForExpiry)
{
alert("Session expired. You will be redirected to login page");
window.location = "Default.aspx";
}
else
{
$.ajax({
type: "POST",
url: 'Default.aspx/PingSession',
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
success: function (msg) {
alert("Your session is now valid.")
}
});
if (warning != null)
{
clearTimeout(warning);
//For warning
warning = setTimeout('SessionWarning()', parseInt(sessionTimeoutWarning) * 60 * 1000);
}
if(timeout != null)
{
clearTimeout(timeout);
//To redirect to the welcome page
timeout = setTimeout('RedirectToWelcomePage()',parseInt(sessionTimeout) * 60 * 1000);
}
}
}
//Session timeout
function RedirectToWelcomePage(){
alert("Session expired. You will be redirected to login page");
window.location = "Default.aspx";
}