這幾天做的網(wǎng)站需要一個(gè)倒計(jì)時(shí),如是作了一個(gè)如下的:
從上面HTML代碼中可以看出,我使用了ASP.NET AJAX 中的Timer控件和JavaScript WebService兩種方法來實(shí)現(xiàn)
ASP.NET AJAX Timer控件:
JavaScript WebService[CountdownService]:
運(yùn)行效果:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Countdown.aspx.cs" Inherits="Countdown" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Countdown</title>
<script type="text/javascript">
var totalSeconds;//剩余時(shí)間(秒)
//倒計(jì)時(shí)函數(shù)
function startCountdown()
{
if(totalSeconds > 0)
{
var days = Math.floor(totalSeconds / 86400);
var hours = Math.floor(totalSeconds % 86400 / 3600);
var minutes = Math.floor(totalSeconds % 3600 / 60);
var seconds = Math.floor(totalSeconds % 60);
document.getElementById("lblDays").innerHTML = days;
document.getElementById("lblHours").innerHTML = (hours >= 10 ? hours : "0" + hours);
document.getElementById("lblMinutes").innerHTML = (minutes >= 10 ? minutes : "0" + minutes);
document.getElementById("lblSeconds").innerHTML = (seconds >=10 ? seconds : "0" + seconds);
totalSeconds--;
}
else
{
clearInterval(timer);
}
}
function pageLoad(sender, args)
{
CountdownService.GetTotalSeconds(onSucceeded);//獲取剩余時(shí)間(秒)
}
//成功獲取剩余時(shí)間后的回調(diào)函數(shù)
function onSucceeded(result)
{
totalSeconds = result;
var days = Math.floor(totalSeconds / 86400);
var hours = Math.floor(totalSeconds % 86400 / 3600);
var minutes = Math.floor(totalSeconds % 3600 / 60);
var seconds = Math.floor(totalSeconds % 60);
document.getElementById("lblDays").innerHTML = days;
document.getElementById("lblHours").innerHTML = (hours >= 10 ? hours : "0" + hours);
document.getElementById("lblMinutes").innerHTML = (minutes >= 10 ? minutes : "0" + minutes);
document.getElementById("lblSeconds").innerHTML = (seconds >=10 ? seconds : "0" + seconds);
}
timer = setInterval("startCountdown()",1000);//開始倒計(jì)時(shí)
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="CountdownService.asmx" />
</Services>
</asp:ScriptManager>
<div style="border-style: solid;">
使用Timer控件
<asp:UpdatePanel ID="UpdatePanelCountdown" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTimerDays" runat="server" Text="0" ForeColor="Red" />天
<asp:Label ID="lblTimerHours" runat="server" Text="0" ForeColor="Red" />時(shí)
<asp:Label ID="lblTimerMinutes" runat="server" Text="0" ForeColor="Red" />分
<asp:Label ID="lblTimerSeconds" runat="server" Text="0" ForeColor="Red" />秒
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick1" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<br />
<hr style="color: Lime;" />
<br />
<div id="DivCountdown" style="border-style: solid;">
使用JavaScript<br />
<asp:Label ID="lblDays" runat="server" Text="0" ForeColor="Red" />天
<asp:Label ID="lblHours" runat="server" Text="0" ForeColor="Red" />時(shí)
<asp:Label ID="lblMinutes" runat="server" Text="0" ForeColor="Red" />分
<asp:Label ID="lblSeconds" runat="server" Text="0" ForeColor="Red" />秒
</div>
</form>
</body>
</html>
從上面HTML代碼中可以看出,我使用了ASP.NET AJAX 中的Timer控件和JavaScript WebService兩種方法來實(shí)現(xiàn)
ASP.NET AJAX Timer控件:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Countdown : System.Web.UI.Page
{
DateTime NowTime;//當(dāng)前時(shí)間
DateTime EndTime = Convert.ToDateTime("2010-06-03 23:59:59");//結(jié)束時(shí)間
TimeSpan CountdownSpan;//時(shí)間間隔
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
NowTime = DateTime.Now;
CountdownSpan = EndTime - NowTime;
if (CountdownSpan.TotalSeconds > 0)
{
lblTimerDays.Text = CountdownSpan.Days.ToString();
lblTimerHours.Text = CountdownSpan.Hours > 10 ? CountdownSpan.Hours.ToString() : "0" + CountdownSpan.Hours.ToString();
lblTimerMinutes.Text = CountdownSpan.Minutes > 10 ? CountdownSpan.Minutes.ToString() : "0" + CountdownSpan.Minutes.ToString();
lblTimerSeconds.Text = CountdownSpan.Seconds > 10 ? CountdownSpan.Seconds.ToString() : "0" + CountdownSpan.Seconds.ToString();
}
}
}
protected void Timer1_Tick1(object sender, EventArgs e)
{
NowTime = DateTime.Now;
CountdownSpan = EndTime - NowTime;
if (CountdownSpan.TotalSeconds > 0)
{
lblTimerDays.Text = CountdownSpan.Days.ToString();
lblTimerHours.Text = CountdownSpan.Hours > 10 ? CountdownSpan.Hours.ToString() : "0" + CountdownSpan.Hours.ToString();
lblTimerMinutes.Text = CountdownSpan.Minutes > 10 ? CountdownSpan.Minutes.ToString() : "0" + CountdownSpan.Minutes.ToString();
lblTimerSeconds.Text = CountdownSpan.Seconds > 10 ? CountdownSpan.Seconds.ToString() : "0" + CountdownSpan.Seconds.ToString();
}
}
}
JavaScript WebService[CountdownService]:
<%@ WebService Language="C#" Class="CountdownService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
[System.Web.Script.Services.ScriptService]
public class CountdownService : System.Web.Services.WebService
{
[WebMethod]
public int GetTotalSeconds()
{
DateTime NowTime = DateTime.Now;
DateTime EndTime = Convert.ToDateTime("2010-06-03 23:59:59");
TimeSpan Countdown = EndTime - NowTime;
return (Countdown.TotalSeconds > 0) ? (int)Countdown.TotalSeconds : 0;
}
}
運(yùn)行效果:
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

