计时器的javascript代码无法正常工作

时间:2015-07-24 12:20:06

标签: javascript c# asp.net timer

我想在.aspx页面上使用计时器功能。

我使用以下代码.aspx页面:

考试开始:

<asp:Label ID="lblCurrTime" runat="server" Font-Bold="True"></asp:Label>
<br />
Time Left: <asp:Label ID="lblExamStTime" runat="server" Font-Bold="True"></asp:Label>

使用的javascript是:

     function myTimer(startVal, interval, outputId, dataField) {
         this.value = startVal;
         this.OutputCntrl = document.getElementById(outputId);
         this.currentTimeOut = null;
         this.interval = interval;
         this.stopped = false;
         this.data = null;
         var formEls = document.form1.elements;
         if (dataField) {
             for (var i = 0; i < formEls.length - 1; i++) {
                 if (formEls[i].name == dataField) {
                     this.data = formEls[i];
                     i = formEls.length + 1;
                 }
             }
         }

         myTimer.prototype.go = function () {
             if (this.value > 0 && this.stopped == false) {
                 this.value = (this.value - this.interval);
                 if (this.data) {
                     this.data.value = this.value;
                 }
                 var current = this.value;
                 this.OutputCntrl.innerHTML = this.Hours(current) + ':' + this.Minutes(current) + ':' + this.Seconds(current);
                 this.currentTimeOut = setTimeout("Timer.go()", this.interval);
             }
             else {
                 alert('Time Out!');
                 window.location('Default.aspx');
             }



         }
         myTimer.prototype.stop = function () {
             this.stopped = true;
             if (this.currentTimeOut != null) {
                 clearTimeout(this.currentTimeout);
             }
         }
         myTimer.prototype.Hours = function (value) {
             return Math.floor(value / 3600000);
         }
         myTimer.prototype.Minutes = function (value) {
             return Math.floor((value - (this.Hours(value) * 3600000)) / 60000);
         }
         myTimer.prototype.Seconds = function (value) {
             var hoursMillSecs = (this.Hours(value) * 3600000)
             var minutesMillSecs = (this.Minutes(value) * 60000)
             var total = (hoursMillSecs + minutesMillSecs)
             var ans = Math.floor(((this.value - total) % 60000) / 1000);

             if (ans < 10)
                 return "0" + ans;

             return ans;
         }
     }
</script>

文件后面的代码:

    // inside pageload func

    if (!IsPostBack)
    {
        this.timerStartValue = long.Parse(ConfigurationManager.AppSettings["Delay"].ToString());
        this.TimerInterval = 500;}


void Page_PreRender(object sender, EventArgs e)
{
    StringBuilder bldr = new StringBuilder();
    bldr.AppendFormat("var Timer = new myTimer({0},{1},{2},'timerData');", this.timerStartValue, this.TimerInterval, this.lblExamStTime.ClientID);
    bldr.Append("Timer.go()");
    ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true);
    ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());
}

void Page_PreInit(object sender, EventArgs e)
{
    string timerVal = Request.Form["timerData"];
    if (timerVal != null || timerVal == "")
    {
        timerVal = timerVal.Replace(",", String.Empty);
        timerStartValue = long.Parse(timerVal);
    }
}
private Int32 TimerInterval
{
    get
    {
        object o = ViewState["timerInterval"];
        if (o != null) { return Int32.Parse(o.ToString()); }
        return 50;
    }
    set { ViewState["timerInterval"] = value; }

}

当我执行此代码时,左边没有出现在浏览器上。此外,我希望计时器从页面加载的先前值恢复。

任何关于dis的建议都将受到赞赏。

0 个答案:

没有答案