code_junkie

Countdown timer on ASP.NET page

Nov 11th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. <asp:ScriptManager ID="ScriptManager1" runat="server">
  2. </asp:ScriptManager>
  3. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  4. <ContentTemplate>
  5. <asp:Label ID="Label1" runat="server">60</asp:Label>
  6. <asp:Timer ID="Timer1" runat="server" Interval="1000"
  7. ontick="Timer1_Tick">
  8. </asp:Timer>
  9. </ContentTemplate>
  10. </asp:UpdatePanel>
  11.  
  12. protected void Timer1_Tick(object sender, EventArgs e)
  13. {
  14. int seconds = int.Parse(Label1.Text);
  15. if (seconds > 0)
  16. Label1.Text = (seconds - 1).ToString();
  17. else
  18. Timer1.Enabled = false;
  19. }
  20.  
  21. <span id="timerLabel" runat="server"></span>
  22.  
  23. <script type="text/javascript">
  24.  
  25. function countdown()
  26. {
  27. seconds = document.getElementById("timerLabel").innerHTML;
  28. if (seconds > 0)
  29. {
  30. document.getElementById("timerLabel").innerHTML = seconds - 1;
  31. setTimeout("countdown()", 1000);
  32. }
  33. }
  34.  
  35. setTimeout("countdown()", 1000);
  36.  
  37. </script>
  38.  
  39. <form name="counter"><input type="text" size="8"
  40. name="d2"></form>
  41.  
  42. <script>
  43. <!--
  44. //
  45. var milisec=0
  46. var seconds=30
  47. document.counter.d2.value='30'
  48.  
  49. function display(){
  50. if (milisec<=0){
  51. milisec=9
  52. seconds-=1
  53. }
  54. if (seconds<=-1){
  55. milisec=0
  56. seconds+=1
  57. }
  58. else
  59. milisec-=1
  60. document.counter.d2.value=seconds+"."+milisec
  61. setTimeout("display()",100)
  62. }
  63. display()
  64. -->
  65. </script>
  66.  
  67. var sec=0 ;
  68. var min=0;
  69. var hour=0;
  70. var t;
  71.  
  72. function display(){
  73. if (sec<=0){
  74. sec+=1;
  75. }
  76. if(sec==60)
  77. {
  78. sec=0;
  79. min+=1;
  80. }
  81. if(min==60){
  82. hour+=1;
  83. min=0;
  84. }
  85.  
  86. if (min<=-1){
  87. sec=0;
  88. min+=1;
  89. }
  90.  
  91. else
  92. sec+=1 ;
  93. document.getElementById("<%=TextBox1.ClientID%>").value=hour+":"+min+":"+sec;
  94. t=setTimeout("display()",1000);
  95. }
  96. window.onload=display;
  97.  
  98. time1 = (DateTime)ViewState["time"] - DateTime.Now;
  99.  
  100. if (time1.TotalSeconds <= 0)
  101. {
  102. Label1.Text = Label2.Text = "TimeOut!";
  103. }
  104. else
  105. {
  106. if (time1.TotalMinutes > 59)
  107. {
  108. Label1.Text = Label2.Text = string.Format("{0}:{1:D2}:{2:D2}",
  109. time1.Hours,
  110. time1.Minutes,
  111. time1.Seconds);
  112. }
  113. else
  114. {
  115. Label1.Text = Label2.Text = string.Format("{0:D2}:{1:D2}",
  116. time1.Minutes,
  117. time1.Seconds);
  118. }
  119. }
  120.  
  121. time1 = (DateTime)ViewState["time"] - DateTime.Now;
  122.  
  123. if (time1.TotalSeconds <= 0)
  124. {
  125. Label1.Text = Label2.Text = "TimeOut!";
  126. }
  127. else
  128. {
  129. if (time1.TotalMinutes > 59)
  130. {
  131. Label1.Text = Label2.Text = string.Format("{0}:{1:D2}:{2:D2}",
  132. time1.Hours,
  133. time1.Minutes,
  134. time1.Seconds);
  135. }
  136. else
  137. {
  138. Label1.Text = Label2.Text = string.Format("{0:D2}:{1:D2}",
  139. time1.Minutes,
  140. time1.Seconds);
  141. }
  142. }
Add Comment
Please, Sign In to add comment