簡體   English   中英

ASP.NET Javascript計時器

[英]ASP.NET Javascript Timer

我在javascript中有一個倒數計時器,由使用asp.net VB的代碼調用。 我找不到追蹤時間的方法。

問題是,我無法獲得回發后經過的時間,因此計時器將繼續計時。 你能幫我嗎,。?? 我真的很感激。,

這是我的代碼片段:

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代碼:

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

我認為您的問題是提交時必須禁用文本框,否則在viewstate中不會跟蹤文本。

該代碼對我有用:

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

后面有代碼:

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

根據您的更新:

問題是您正在按鈕的Click事件處理程序中設置按鈕的OnClientClick屬性。

這基本上意味着,除非單擊該按鈕,否則它將沒有任何作用,然后對您沒有用。

將OnClientClick設置為OnInit或Page_Load之類的位置,或者像我的示例一樣在aspx中進行設置。 這對您有意義嗎? 當您單擊按鈕時,必須已設置OnClientClick屬性。

如果您打算做任何ASP.NET工作,那么您真的想閱讀並真正了解頁面生命周期。 出發點:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM