【问题标题】:ASP.NET Javascript TimerASP.NET Javascript 计时器
【发布时间】:2010-10-10 12:58:08
【问题描述】:

我在 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

【问题讨论】:

  • 您遇到的问题到底是什么,是否在回发时保持时间不变?你放在那里的代码对我来说是初始页面加载,而不是在回发中。
  • 是的,!这实际上是问题所在。,我无法得到回发后经过的时间。你能帮帮我吗?,?
  • 很抱歉,如果我在第一次发布我的代码时没有详细说明,。你的算法和我的算法是一样的(^_^),。你可以再看看我的帖子,。我已编辑 (^_^),.
  • 你很接近,检查我更新的解释。我认为这应该可以为您解决问题,如果确实如此,请务必将问题标记为已回答。

标签: asp.net javascript


【解决方案1】:

我认为您的问题是您必须在提交时取消禁用文本框,否则文本不会在视图状态中被跟踪。

此代码适用于我:

<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 的工作,您真的很想阅读并真正了解页面生命周期。起点:

【讨论】:

  • 非常感谢。,!!!我做到了。,!!!哇哈哈.,!!!很高兴。,!!!我刚刚在页面加载时添加了“NextBtn.Attributes.Add("onclick", "mySubmit()")"。非常感谢。对此,我真的非常感激,。!!!多年来,我一直在解决这个问题。,呵呵,。再次感谢你,。
  • 没问题,您应该点击答案旁边的复选框将其标记为完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多