Loading ...

Display clock in textbox in javascript

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  client side web development   » Display clock in textbox in javascript

Display clock in textbox in javascript

Posts under the topic: Display clock in textbox in javascript

Posted: 11/28/2009

Lurker 170  points  Lurker
  • Joined on: 10/17/2009
  • Posts: 34

I have a textbox in asp.net page i want to show clock continiously in that textbox.


tags javascript

Posted: 11/29/2009

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

In ASPX

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
<form runat="server">
    <asp:TextBox ID="txtClock" runat="server" style="width:400px;"></asp:TextBox>
</form>    
</body>

<script type="text/javascript">

    //Users time
    var timeLocal = new Date();

    //Calculate the difference (returns milliseconds)
    millDiff = timeLocal - timeServer;

    //initalize the clock on loading of page
    window.onload = function()
    {
        //set the interval so clock ticks
        var timeClock = setInterval('TimeTick()', 10);
    }

    //The ticking clock function
    function TimeTick()
    {
        //grab updated time
        timeLocal = new Date();
        //add time difference
        timeLocal.setMilliseconds(timeLocal.getMilliseconds() - millDiff);
        //display the value
        document.getElementById('<%= txtClock.ClientID %>').value = timeLocal;
    }

</script>

</html>


In Codebehind:

using System;

namespace TestProject
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(string.Format("<script>var timeServer = new Date('{0}');</script>", DateTime.Now));

        }

    }

}



Page 1 of 1 (2 items)