时间倒计时

        /// <summary>
        /// 时间倒计时
        /// </summary>
        /// <param name="str">时间(比当前时间要晚)</param>
        /// <param name="counts">变量(变量不同可以设置多个倒计时)</param>

       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;
            //Response.Write(simTime);

            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();
        }

显示到那里,就把层插入到那里.

<div id="times"+counts +"">距抢购时间还有00时00分00秒</div>

posted @ 2008-11-27 12:18  冬季开花  阅读(335)  评论(1)    收藏  举报