gpt4 book ai didi

每秒在 ASP.net 头部创建 Javascript,Ajax Timer 导致问题

转载 作者:行者123 更新时间:2023-11-29 15:57:12 24 4
gpt4 key购买 nike

我有一个简单的表单,它使用当前时间的计时器更新标签。我注意到,当代码运行时,它会在每个滴答声中将以下内容添加到页眉。

<script type="text/javascript">Sys.Application.add_init(function() {
$create(Sys.UI._Timer, {"enabled":true,"interval":1000,"uniqueID":"Timer1"}, null, null, $get("Timer1"));
});
</script>

这正常吗?我该如何阻止呢?因此,内存慢慢爬升,几千秒后页面陷入停滞。我在网上寻找答案,但没有任何帮助。

这是我的标记:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm4.aspx.vb" Inherits="A2Chat.WebForm4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000"></asp:Timer>
<asp:Label id="Label1" runat="server" Text="test"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

下面是代码:

Public Class WebForm4
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs)
Label1.Text = Now()
End Sub

End Class

当我在 Chrome 中检查页面时,我看到了 this .

Every second this block of code is added and I didn't actually add it but the system is adding it

任何遇到此问题的人的 future 引用

将计时器移到更新面板之外。如果您需要更新面板内的计时器,那么以下内容将带您到达那里......

                    <script type="text/javascript">
window.onload = function () {
setInterval(function () {
document.getElementById("<%=Button7.ClientID %>").click();
}, 1000);
};
</script>

最佳答案

将计时器移到 UpdatePanel 之外。这将停止在每次更新 UpdatePanel 时重新添加计时器脚本。

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm4.aspx.vb" Inherits="A2Chat.WebForm4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:Timer ID="Timer1" runat="server" Interval="1000"></asp:Timer>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
<ContentTemplate>
<asp:Label id="Label1" runat="server" Text="test"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

代码隐藏:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = Now()
UpdatePanel1.Update()
End Sub

关于每秒在 ASP.net 头部创建 Javascript,Ajax Timer 导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57470422/

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