Hello People!
Hope, you solve my problem.

I program in VB.NET and have a WebForm that contain a client Javascript counter (min:sec) to zero. I use this code to show up a counter.

<script language="javascript">

var up,down;
var min1,sec1;
var cmin1,csec1,cmin2,csec2;

function Minutes(data) {
for(var i=0;i<data.length;i++) if(data.substring(i,i+1)==":") break;
return(data.substring(0,i)); }

function Seconds(data) {
for(var i=0;i<data.length;i++) if(data.substring(i,i+1)==":") break;
return(data.substring(i+1,data.length)); }

function Display(min,sec) {
var disp;
if(min<=9) disp=" 0";
else disp=" ";
disp+=min+":";
if(sec<=9) disp+="0"+sec;
else disp+=sec;
return(disp); }

function Down() {
cmin2=1*Minutes(document.sw.beg2.value);
csec2=0+Seconds(document.sw.beg2.value);
DownRepeat(); }

function DownRepeat() {
csec2--;
if(csec2==-1) { csec2=59; cmin2--; }
document.sw.disp2.value=Display(cmin2,csec2);
if((cmin2==0)&&(csec2==0)) alert("Timer-CountDown Stopped");
else down=setTimeout("DownRepeat()",1000); }
// End -->

</script>

Function is called from here: <body onload="Down()".... >
And a value I get from the server session variable (Session("myLimit"))
It described in this client container.

<form name="sw">

<INPUT id=timeToCount style="Z-INDEX: 101; LEFT: 40px; VISIBILITY: hidden; POSITION: absolute; TOP: 408px" 
readOnly type=text size=3 value='<%=Session("myLimit")%>' name=beg2> 
<INPUT id="myCount" style="Z-INDEX: 103; LEFT: 32px; WIDTH: 61px; POSITION: absolute; TOP: 344px; HEIGHT: 22px"  readOnly type="text" size="4" name="disp2">

</form>

The timer is functioning well. For example, if session ("myLimit") = 5, then on WebForm load it begin counting 4:59 to zero.

What I need:

  1. Add a counter pause/unpause button.
  2. Need this timer to survive postbacks (I have another button on the form and on press it fire postback and reset a counter)
  3. When counter reaches zero I have to do any server side command (for example, Label2.Text="Time is up" , where label2 is a server control)

Maybe I ask too much, but if you can help me with these 3 :sad:

> Add a counter pause/unpause button.

Set a Javascript variable which would be checked before invoking your countdown function. As soon as the 'Pause' button is clicked, set the flag variable, store the state of your counter variables and clear the timeout. As soon as the 'UnPause' button is clicked, just unset the flag, start the timeout again with the previous saved counter state.

> Need this timer to survive postbacks (I have another button on the form and on press it
> fire postback and reset a counter)

Save the value of the updated counter in your session variable and read it again when the page loads. As far as reading the updated counter values at the server is concerned, save the state of the counter variable in a hidden field and at the server do something along the lines of Request("updatedState").

> When counter reaches zero I have to do any server side command (for example,
> Label2.Text="Time is up" , where label2 is a server control)

This might be a better answered in the VB.NET forums since Javascript as such doesn't understand any 'server controls'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.