AJAX - ASP.NET - 计时器延迟问题

发布于 2024-08-30 11:19:34 字数 1174 浏览 1 评论 0原文

我正在尝试制作一个网络应用程序,您可以在其中看到 Ajax 倒计时器。每当我按下按钮时,倒计时就会回到 30 并继续倒计时。

现在的问题是,每当我按下按钮时,计时器就会持续倒计时一到两秒,之后的大多数时间,计时器都会在 30 上保持很长时间。

网页表单代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="Label1" runat="server" Text="geen verbinding"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>

</form>

代码隐藏:

static int timer = 30;
protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = timer.ToString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
    timer--;

}
protected void Button1_Click(object sender, EventArgs e)
{
    timer = 30;         
}

希望有人知道问题是什么以及是否有解决办法。

提前致谢!

I'm trying to make an webapplication where you see an Ajax countdown timer. Whenever I push a button the countdown should go back to 30 and keep counting down.

Now the problem is whenever I push the button the timer keeps counting down for a second or 2 and most of the time after that the timer keeps standing on 30 for to long.

WebForm code:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="Label1" runat="server" Text="geen verbinding"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>

</form>

Code Behind:

static int timer = 30;
protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = timer.ToString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
    timer--;

}
protected void Button1_Click(object sender, EventArgs e)
{
    timer = 30;         
}

Hope somebody knows what the problem is and if there is anyway to fix this.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷荒 2024-09-06 11:19:34

为什么不完全在客户端实现计时器?你能解释一下为什么它必须是回调吗?它需要在服务器上做什么?

<script type="text/javascript" language="javascript">
var timer = 0;
function resetTimer() {
    timer = 0;
    timerTick();
}
function timerTick() {
    var target = document.getElementById("timerResult");

    target.innerHTML = timer++;
    window.setTimeout("timerTick()", 1000);
}
</script>

在正文标签中...

<body onload="timerTick();">

以及要显示的地方

<div id="timerResult" >timerResult</div>

您只需添加一个按钮,调用resetTimer()。我把它留给你

Why don't you implement the Timer entirely on the ClientSide? Can you explain why it has to be a callback? What does it need to do on the Server?

<script type="text/javascript" language="javascript">
var timer = 0;
function resetTimer() {
    timer = 0;
    timerTick();
}
function timerTick() {
    var target = document.getElementById("timerResult");

    target.innerHTML = timer++;
    window.setTimeout("timerTick()", 1000);
}
</script>

And in the body tag...

<body onload="timerTick();">

And somewhere to display

<div id="timerResult" >timerResult</div>

You justy have to add a button chich calls resetTimer(). I'll leave that to you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文