Javascript计时器无法正常工作

时间:2014-12-17 06:45:38

标签: javascript asp.net

请看下面的代码我在asp页面中添加了一个javascript计时器,但是每个按钮点击计时器都在快速和快速地运行。快点。我正在通过更新面板下的按钮点击调用javascript。

protected void Button1_Click(object sender, EventArgs e)
{


    string timerVal = txtResult.Value;

    timerVal = log.converttosec(timerVal);

    if (timerVal != null || timerVal == "")
    {
        //  string newa = timerData.Value;
        timerVal = timerVal.Replace(",", String.Empty);
        timerStartValue = long.Parse(timerVal);
    }

    StringBuilder bldr = new StringBuilder();
    bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerdata');", this.timerStartValue, this.TimerInterval, this.txtResult.ClientID);
    bldr.Append("Timer.go()");

    ScriptManager.RegisterStartupScript(update1, update1.GetType(), Guid.NewGuid().ToString(), bldr.ToString(), true);
    ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());
}

间隔功能。

private Int32 TimerInterval
{
    get
    {
        object o = ViewState["timerInterval"];
        if (o != null) { return Int32.Parse(o.ToString()); }
        return 50;
    }
    set { ViewState["timerInterval"] = value; }

}

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.documentElement;
             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.value = this.Hours(current) + ':' + this.Minutes(current) + ':' + this.Seconds(current);
                     this.currentTimeOut = setTimeout("Timer.go()", this.interval);
                 }
                 else {
                     alert('Time Out!');
                     window.location('Index.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;
             }
         }

每次点击计时器都会越来越快。为什么它比简单的间隔更快?感谢。

0 个答案:

没有答案