Link to home
Start Free TrialLog in
Avatar of egdigital
egdigital

asked on

Dynamic Javascript timer

I am writing an application using asp.net and want to display a timer on the page.  the value that counts down on the time changes so i need a way to pass in a time dynamically into the timer.
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

is this what you want?

Countdown Timer Javascript
http://java-scripts.net/javascripts/Countdown-Timer.phtml

have a look at the example in the above link

Rejo
if yes, then from the code in the above link the start time is hardcoded to "30"
document.counter.d2.value='30'
We can dynamically assign values from code-behind using
document.counter.d2.value='<%=myValue%>';

and myValue can be set in page_load ..

there are other ways to do the same thing .. that is, passing this value as a parameter to the function, using hidden texboxes etc .. depends on what exactly you are trying to implement

Rejo


Avatar of egdigital
egdigital

ASKER

I tried this solution however the timer is not even starting.  I think its because I am using an ascx file for this which does not have a form tag.  The example here http://java-scripts.net/javascripts/Countdown-Timer.phtml uses form with the name "counter".  I have tried the following but the object is still null:

<script type="text/javascript">
<!--
var milisec=0
var seconds=30
document.d2.value='30'
//document.getElementById("d2").value='30'

function display(){
 if (milisec<=0){
    milisec=9
    seconds-=1
 }
 if (seconds<=-1){
    milisec=0
    seconds+=1
 }
 else
    milisec-=1
    document.d2.value=seconds+"."+milisec
    setTimeout("display()",100)
}
display()
-->
</script>

<input type="text" size="8" id="d2" name="d2" />

ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial