简体   繁体   English

如何在asp.net中修复Javascript计时器(母版页概念)

[英]How to fix Javascript Timer In asp.net(Master Page concept)

I Am trying to add Javascript timer in my asp.net project, it is implemented and runing as i needed, but if am pressing any key in that it is disappering for me. 我试图在我的asp.net项目中添加Javascript计时器,该计时器已实现并根据需要运行,但是如果按任何键,它将对我不利。 But Timer is runing. 但是计时器正在运行。

Js. Js。 code

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.innerHTML = 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;
         }
     }         

I'm calling it from button control. 我从按钮控件调用它。

Code

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

}

 protected void Button1_Click(object sender, EventArgs e)
{


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

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.lblTimerCount.ClientID);
    bldr.Append("Timer.go()");
    ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true);
    ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());
}

I'M displaying it into Label control. 我将其显示到Label控件中。

 <asp:Label ID="lblTimerCount" runat="server" Height="27px" 
                 Width="232px"></asp:Label>

In Page_PreInit 'string timerVal' value is coming same after page_load which I'M passing, it starting timer from starting.? 在Page_PreInit中的“ string timerVal”值在我传递的page_load之后相同时,它从启动开始计时。

Please correct me. 请纠正我。

These are the simple steps you need to follow: 1) upon page load, set the timerinterval on a hidden element 这些是您需要遵循的简单步骤:1)在页面加载时,在隐藏元素上设置timerinterval

Your html should look like this: 您的html应该如下所示:

<input type="button" id="btnStartTimer" name="StartTimer" value="Start" />
<input type="hidden" id="hdnInterval" name="Interval" value="50" />
<p>
    <label>Show Timer:</label>
    <label id="lblOutput"></label>
</p>

$('#YourButtonId').on('click', function(e){e.preventDefault; Timer.Go(); });

Your button js click event should look like this: 您的按钮js click事件应如下所示:

$('#btnStartTimer').on("click", function (e) {
    e.preventDefault();
    var interval = $('#hdnInterval').val();
    var timer = new myTimer(0, interfal, "lblOutput", null);
    timer.go();
});

Something along those lines. 遵循这些原则。 This will make post backs unnecessary 这将使回发不必要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM