gpt4 book ai didi

回发期间 Javascript 检测

转载 作者:行者123 更新时间:2023-11-28 01:43:53 25 4
gpt4 key购买 nike

我在 ASP.NET Web 窗体站点上实现了一个系统,用于将通知传递给客户端。通知可以是验证消息、成功消息、警告、错误等。这些消息由服务器端的代码生成,然后通过客户端的脚本从服务器检索并显示。

在我网站的每个页面上,我都嵌入了一个 JavaScript 函数,该函数通过 jQuery AJAX 调用 Web 服务(在服务器端)并请求最新通知。如果有通知,则会使用 Noty 显示它们。在服务器端,已发送到客户端的消息将从队列中删除。

这个效果非常好。然而,假设我在一个名为 NewEmployee.aspx 的页面上,他们通过填写新表单来创建一名新员工,这会生成某种通知,例如“使用 ID 58478 创建的新员工”,并将他们带到查看Employee.aspx 页面。如果回发发生得足够慢,则当用户仍在 NewEmployee.aspx 上但在用户到达 ViewEmployee.aspx 之前,消息将开始显示。最终结果是通知仅显示一瞬间,用户永远看不到它。

我需要一种在客户端使用 JavaScript 的方法来检测页面是否正在执行回发。如果我有这个,我可以阻止它在回发期间调用 Web 服务并让它等待它完成。我希望它看起来像这样。

setInterval(oneSecondFunction, 1000); //check for messages every 1 second

function oneSecondFunction()
{
var IsPostingBackRightNow=GetIsPostingBackStatus(); //I need your help writing this function
if(!IsPostingBackRightNow)
{
$.ajax({
type: 'POST',
url: 'myurl/MessageService.asmx/GetCurrentMessage',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ sessionid: sessionid, locale: 'en-US' }),
dataType: 'xml',
success: function (msg)
{
//show message via noty if there's a message
}
});
}
}

最佳答案

回发是从表单传递的,因此您捕获表单上的onsubmit事件,然后打开您的标志。

这是如何...将其添加到后面的代码

Page.Form.Attributes["onsubmit"] = "return GoForPostBack();";

让此属性也呈现在您的表单上...

<form ... onsubmit="return GoForPostBack();">

和 JavaScript

var IsPostingBackRightNow = false;
function GoForPostBack()
{
IsPostingBackRightNow = true;
return true;
}


setInterval(oneSecondFunction, 1000); //check for messages every 1 second

function oneSecondFunction()
{
if(!IsPostingBackRightNow)
{
$.ajax({
type: 'POST',
url: 'myurl/MessageService.asmx/GetCurrentMessage',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ sessionid: sessionid, locale: 'en-US' }),
dataType: 'xml',
success: function (msg)
{
//show message via noty if there's a message
}
});
}
}

关于回发期间 Javascript 检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20553383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com