简体   繁体   English

ASP.NET Javascript计时器

[英]ASP.NET Javascript Timer

I have a countdown timer in javascript that is called by a code behind using asp.net VB. 我在javascript中有一个倒数计时器,由使用asp.net VB的代码调用。 I cannot find a way to keep track of the time ,. 我找不到追踪时间的方法。

the problem is., I cannot get the time that elapsed after postback so that the timer would continue ticking,. 问题是,我无法获得回发后经过的时间,因此计时器将继续计时。 can you help me please,.?? 你能帮我吗,。?? I would really appreciate it., 我真的很感激。,

here is my code fragment: 这是我的代码片段:

asp.net VB codebehid asp.net VB代码隐藏

    on page load{
        If Page.IsPostBack = True Then
            ClientScript.RegisterClientScriptBlock(Me.GetType, "timer_script",
           "<script language='javascript'>function mySubmit()</script>")

            ClientScript.RegisterStartupScript(Me.GetType, "timer_script", 
            "<script>countdown_clock();</script>")
        End If

        If Page.IsPostBack = false Then
            TimerTxtbx.Text = "00:06:10"   'hour:min:sec -> initialize timer
            ClientScript.RegisterClientScriptBlock(Me.GetType, "timer_script",
           "<script language='javascript'>function mySubmit()</script>")

            ClientScript.RegisterStartupScript(Me.GetType, "timer_script", 
            "<script>countdown_clock();</script>")
        End If
    }

    on button click{
        NextBtn.Attributes.Add("onclick", "mySubmit()")   'call a javascript function

        ClientScript.RegisterStartupScript(Me.GetType, "timer_script", 
        "<script>countdown_clock();</script>")
    }

javascript code: JavaScript代码:

function mySubmit()
{
    document.getElementById('TimerTxtbx').removeAttribute('disabled');
}

    function countdown_clock()
{                   
    var current_time;
    current_time = document.getElementById('TimerTxtbx').value;


    var hours = current_time.substring(0,2);
    var minutes = current_time.substring(3,5);
    var seconds = current_time.substring(6,8);  

    var n_seconds;
    var n_minutes = minutes;
    var n_hours = hours;

    if (seconds == 0)
    {
       n_seconds = 59;

       if (minutes == 0)
        {
        n_minutes = 59;
            if (hours == 0){
            alert('Time is up');
            return;
            }
                else{   
                n_hours = hours - 1;
               if (n_hours < 10)
                   n_hours = "0" + n_hours;
                }
            }
        else
        {
            n_minutes = minutes - 1;
            if (n_minutes < 10)
                n_minutes = "0" + n_minutes;
        }
    }
    else
    {
        n_seconds = seconds - 1;
        if (n_seconds < 10)
            n_seconds = "0" + n_seconds;
    }


      document.getElementById('TimerTxtbx').value = n_hours + ':' + n_minutes + ':' + n_seconds;

      setTimeout("countdown_clock()",1000); //function call and delay by 1sec

      document.getElementById('TimerTxtbx').setAttribute('disabled','disabled');
      }//end function

I think your problem is that you have to undisable the textbox when you submit, otherwise the text doesn't get tracked in the viewstate. 我认为您的问题是提交时必须禁用文本框,否则在viewstate中不会跟踪文本。

This code works for me: 该代码对我有用:

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="TimerTxtbx" runat="server" />
    <asp:Button runat="server"  OnClientClick="mySubmit()"/>
</div>
</form>

With code behind: 后面有代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(typeof (Page), "timer_script",
                                           "<script>countdown_clock();</script>");

        if (!IsPostBack)
        {
            TimerTxtbx.Text = "00:00:10";
        }
    }

Update based on yours: 根据您的更新:

The problem is that you are setting the OnClientClick property of your button in the Click event handler for your button. 问题是您正在按钮的Click事件处理程序中设置按钮的OnClientClick属性。

This basically means it won't have any effect until the button is clicked, and then it isn't useful to you. 这基本上意味着,除非单击该按钮,否则它将没有任何作用,然后对您没有用。

Set the OnClientClick somewhere like OnInit or Page_Load, or just do it in the aspx like in my example. 将OnClientClick设置为OnInit或Page_Load之类的位置,或者像我的示例一样在aspx中进行设置。 Does this makes sense to you? 这对您有意义吗? When you click the button the OnClientClick property must have already been set. 当您单击按钮时,必须已设置OnClientClick属性。

If you're going to do any ASP.NET stuff, you really want to readup and really get to know the page lifecycle. 如果您打算做任何ASP.NET工作,那么您真的想阅读并真正了解页面生命周期。 Starting points: 出发点:

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

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