怎么用缓存实现倒计时功能

w398687283 2009-07-24 07:04:13
做一个倒计时的功能,就是在数据库里边设置了结束日期,访问页面的时候用结束日期和当前日期的差做倒计时,每一秒刷新一下这个时差,类似购物网站的限时购买
我现在已经计算出了时差,但是必须刷新一下页面的时候时间才刷新,怎么能不刷新页面让那个时间动起来,记得以前用缓存可以实现这种效果,现在不知道怎么用了
如果有更好的方法的话也可以啊,请各位赐教,多谢了!!!
...全文
282 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
cat_hsfz 2009-07-28
  • 打赏
  • 举报
回复
你可以用JavaScript的setTimeout/setInterval来定期更新客户端时间。
stromboy007 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 xuweijie555 的回复:]
放个UpdatePanel 在放个Timer 就行了,不会刷了忽忽
[/Quote]

这样子的最好要调用服务器控件。。。。直接JS就可以了 如果要调用服务器时间的话可以把服务器时间取出来,任何没1秒钟加1就行
wangan2008 2009-07-28
  • 打赏
  • 举报
回复
up
slund 2009-07-28
  • 打赏
  • 举报
回复
用Js可以实现。
zhulong1111 2009-07-28
  • 打赏
  • 举报
回复
放个UpdatePanel 在放个Timer 就行了 设置下timer就行了 哦
bindian 2009-07-27
  • 打赏
  • 举报
回复
//页面代码
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >



<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="500"
ontick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>




//cs代码
protected void Timer1_Tick(object sender, EventArgs e)
{
this.Label2.Text = DateTime.Now.ToString();
}
Kkiki 2009-07-27
  • 打赏
  • 举报
回复
xuyiazl 2009-07-27
  • 打赏
  • 举报
回复
反正不管如何,你在服务端是要进行判断的!
xuyiazl 2009-07-27
  • 打赏
  • 举报
回复

在页面加载的时候就已经把这两个时间取出来了.

再利用javascript进行时间差计算.
红街咖啡 2009-07-27
  • 打赏
  • 举报
回复
用timer不是??倒计时?用JavaScript得了。
w398687283 2009-07-25
  • 打赏
  • 举报
回复
用了Timer ,给2位正确答案各50分,多谢啦
w398687283 2009-07-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
通过ajax实现倒计时
this.TextBox1.Text=System.DateTime.Now.ToString();
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
   
    </div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick">
                </asp:Timer>
                <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
            </ContentTemplate>
         
        </asp:UpdatePanel>
    </form>
</body>

protected void WriteJs(string str, int counts)
        {
            TimeSpan sumTime = Convert.ToDateTime(str).Subtract(Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString()));            Double simTime = sumTime.TotalSeconds;
            SB.Append(" <script language=\"javascript\" type=\"text/javascript\">");
            SB.Append("var h" + counts + ",m" + counts + ",s" + counts + ",hstr" + counts + ",mstr" + counts + ",sstr" + counts + ",timestr" + counts + ";");
            SB.Append("var etime" + counts + " = " + simTime + "; ");
            SB.Append("function timer"+counts+"()");
            SB.Append("{");
            SB.Append("    h" + counts + " = Math.floor(etime" + counts + " / 3600);");
            SB.Append("    m" + counts + " = Math.floor(etime" + counts + " / 60) % 60;");
            SB.Append("    s" + counts + " = Math.floor(etime" + counts + " % 60);");
            SB.Append("    h" + counts + " < 0 ? h" + counts + " = 0 : h" + counts + " = h" + counts + ";");
            SB.Append("    m" + counts + " < 0 ? m" + counts + " = 0 : m" + counts + " = m" + counts + ";");
            SB.Append("    s" + counts + " < 0 ? s" + counts + " = 0 : s" + counts + " = s" + counts + ";");
            SB.Append("    h" + counts + ".toString().length < 2 ? hstr" + counts + " = \"0\" + h" + counts + ".toString() : hstr" + counts + " = h" + counts + ";");
            SB.Append("    m" + counts + ".toString().length < 2 ? mstr" + counts + " = \"0\" + m" + counts + ".toString() : mstr" + counts + " = m" + counts + ";");
            SB.Append("    s" + counts + ".toString().length < 2 ? sstr" + counts + " = \"0\" + s" + counts + ".toString() : sstr" + counts + " = s" + counts + ";");
            SB.Append("    timestr" + counts + " = hstr" + counts + " + \"时\" + mstr" + counts + " + \"分\" + sstr" + counts + " + \"秒\";");
            SB.Append("    document.getElementById(\"times" + counts + "\").innerText = '距时间还有:'+timestr" + counts + ";");
            SB.Append("    etime" + counts + " = etime" + counts + " - 1;");
            SB.Append("    if(timestr" + counts + "=='00时00分00秒') window.location.reload();");
            SB.Append("    setTimeout('timer" + counts + "()',1000);");
            SB.Append("}");
            SB.Append("timer" + counts + "();");
            SB.Append(" </script>");

            TimerJs.Text = SB.ToString();
        }
参考


[/Quote]
我用<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick">
</asp:Timer>
并没有每秒减一啊,改怎么设置
w398687283 2009-07-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhaoqiliang527 的回复:]
up
[/Quote]
时间并没有刷新啊
奶油果冻加冰 2009-07-25
  • 打赏
  • 举报
回复
放个UpdatePanel 在放个Timer 就行了,不会刷了忽忽
plglenn 2009-07-24
  • 打赏
  • 举报
回复
d
zhaoqiliang527 2009-07-24
  • 打赏
  • 举报
回复
up
wuyq11 2009-07-24
  • 打赏
  • 举报
回复
通过ajax实现倒计时
this.TextBox1.Text=System.DateTime.Now.ToString();
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>

</asp:UpdatePanel>
</form>
</body>

protected void WriteJs(string str, int counts)
{
TimeSpan sumTime = Convert.ToDateTime(str).Subtract(Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString())); Double simTime = sumTime.TotalSeconds;
SB.Append("<script language=\"javascript\" type=\"text/javascript\">");
SB.Append("var h" + counts + ",m" + counts + ",s" + counts + ",hstr" + counts + ",mstr" + counts + ",sstr" + counts + ",timestr" + counts + ";");
SB.Append("var etime" + counts + " = " + simTime + "; ");
SB.Append("function timer"+counts+"()");
SB.Append("{");
SB.Append(" h" + counts + " = Math.floor(etime" + counts + " / 3600);");
SB.Append(" m" + counts + " = Math.floor(etime" + counts + " / 60) % 60;");
SB.Append(" s" + counts + " = Math.floor(etime" + counts + " % 60);");
SB.Append(" h" + counts + " < 0 ? h" + counts + " = 0 : h" + counts + " = h" + counts + ";");
SB.Append(" m" + counts + " < 0 ? m" + counts + " = 0 : m" + counts + " = m" + counts + ";");
SB.Append(" s" + counts + " < 0 ? s" + counts + " = 0 : s" + counts + " = s" + counts + ";");
SB.Append(" h" + counts + ".toString().length < 2 ? hstr" + counts + " = \"0\" + h" + counts + ".toString() : hstr" + counts + " = h" + counts + ";");
SB.Append(" m" + counts + ".toString().length < 2 ? mstr" + counts + " = \"0\" + m" + counts + ".toString() : mstr" + counts + " = m" + counts + ";");
SB.Append(" s" + counts + ".toString().length < 2 ? sstr" + counts + " = \"0\" + s" + counts + ".toString() : sstr" + counts + " = s" + counts + ";");
SB.Append(" timestr" + counts + " = hstr" + counts + " + \"时\" + mstr" + counts + " + \"分\" + sstr" + counts + " + \"秒\";");
SB.Append(" document.getElementById(\"times" + counts + "\").innerText = '距时间还有:'+timestr" + counts + ";");
SB.Append(" etime" + counts + " = etime" + counts + " - 1;");
SB.Append(" if(timestr" + counts + "=='00时00分00秒') window.location.reload();");
SB.Append(" setTimeout('timer" + counts + "()',1000);");
SB.Append("}");
SB.Append("timer" + counts + "();");
SB.Append("</script>");

TimerJs.Text = SB.ToString();
}
参考

gaijf 2009-07-24
  • 打赏
  • 举报
回复
用js做表现,异步调用一个服务端处理,也就是流行的ajax效果
chen_ya_ping 2009-07-24
  • 打赏
  • 举报
回复
这个js来也可以实现的
w398687283 2009-07-24
  • 打赏
  • 举报
回复
没人自己顶,不要沉下去

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧