Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function within a function

Status
Not open for further replies.

JerryH

Programmer
Jul 8, 2000
15
0
0
US
I'm attempting to build a second timer using JavaScript. The following code works:

Code:
<script language="JavaScript1.2" type="text/javascript">
counts=10
var start=new Date();
start=Date.parse(start)/1000;
function CountIt(){
  var now=new Date();
  now=Date.parse(now)/1000;
  var x=parseInt(counts-(now-start),10);
  if(document.form1){document.form1.clock.value = x;};
  if(x>0){
    timerID=setTimeout("CountIt()", 100);
  }
}
window.setTimeout("CountIt()",100);
</script>

But once I embed the function within another, it stops working.

Code:
<script language="JavaScript1.2" type="text/javascript">
function CountDown(counts){
  var start=new Date();
  start=Date.parse(start)/1000;
  function CountIt(){
    var now=new Date();
    now=Date.parse(now)/1000;
    var x=parseInt(counts-(now-start),10);
    if(document.form1){document.form1.clock.value = x;};
    if(x>0){
      timerID=setTimeout("CountIt()", 100);
    }
  }
  window.setTimeout("CountIt()",100);
}
CountDown(10);
</script>

The error is that the function CountIt is undefined. I understand that accessing CountIt from outside CountDown is difficult but in the example above - all calls to CountIt is inside the CountDown function. Does anyone know of a fix?
 

CountIt is not defined as far as the timeout call is concerned, as it is NOT a global function. Move the CountIt function out of the CountDown function to make it global, and avoid that issue.

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 

Of course, you will have to re-jig your code structure to either make "counts" and "start" a global variable, or pass them into CountIt each time.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
I thought of moving CountIt out of CountDown last night and was having problems with counts being global (just as you said BillyRay). I've reworked the code this morning and here is the result:

Code:
<script language="JavaScript1.2" type="text/javascript">
function CountIt(){
  var now=new Date();
  now=Date.parse(now)/1000;
  var x=parseInt(counts-(now-start),10);
  if(document.form1){document.form1.clock.value = x;};
  if(x>0){
    timerID=setTimeout("CountIt()", 100);
  }
}
function CountDown(num){
  counts=num
  start=new Date();
  start=Date.parse(start)/1000;
  CountIt();
  window.setTimeout("CountIt()",100);
}
CountDown(10);
</script>

It seems to be working! Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top