Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764

    JavaScript Error "not defined" and "not implemented"

    I have a fairly simple count-down timer, which looks something like this:

    index.php
    HTML Code:
    <form name="dw">
     Start in <input type="text" name="counter" size="3" value="30" style="text-align: center;" /> seconds ...
    </form>
    globalFunctions.js
    HTML Code:
    function countdown()
    {
    	if (document.dw) {
    		x = dw.counter.value;
    		x--;
    		dw.counter.value = x;
    		if (x > 0) {
    			setTimeout("countdown()",1000);
    		} else {
    			loadcaptcha();
    		}
    	}
    }
    window.onload = setTimeout("countdown()",1000);
    This above code works fine in IE7, but it gives the error "Not Implemented".

    In the FireFox browser, it does not work at all and gives me the following error:

    Error: dw is not defined
    Source File: globalFunctions.js
    Line: 65
    Where im i going wrong? Can someone point me in the right direction please?

  2. #2
    Join Date
    Oct 2004
    Location
    UK
    Posts
    487
    Best bet is to use DOM (getElementById())

    HTML Code:
    <form name="dw" id="dw">
     Start in <input type="text" name="counter" id="counter" size="3" value="30" style="text-align: center;" /> seconds ...
    </form>
    Code:
    function countdown()
    {
        var formElement = document.getElementById('dw');
        var counterElement = document.getElementById('counter');
        if (formElement)
        {
            x = counterElement.value;
            x--;
            counterElement.value = x;
            if (x > 0)
            {
                setTimeout("countdown()",1000);
            } else
            {
                loadcaptcha();
            }
        }
    }
    window.onload = setTimeout("countdown()",1000);

  3. #3
    Join Date
    Jan 2006
    Location
    Athens, Greece
    Posts
    1,481
    <form name="dw" id="dw">

  4. #4
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    Thanks for the prompt reply guys. Now, again, this new code works in IE7 but with the following error again "Not Implemented".

    In firefox, the code does not work at all and im getting this new error:

    Error: counterElement has no properties
    Source File: globalFunctions.js
    Line: 68
    Any idea what's wrong now?

  5. #5
    Join Date
    Jan 2006
    Location
    Athens, Greece
    Posts
    1,481
    Add an id to the input text tag.

  6. #6
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    Aww nice one, thanks for that, script works great

  7. #7
    Join Date
    Mar 2004
    Location
    USA
    Posts
    4,345
    I like to use spans:

    Code:
    <script>
    function countdown(){
    	x = parseInt(document.getElementById('shownum').innerHTML);
    	x--;
    	document.getElementById('shownum').innerHTML = x;
    	if (x > 0){
    		setTimeout("countdown()",1000);
    	} else {
    		loadcaptcha();
    	} 
    }
    window.onload = setTimeout("countdown()",1000);
    </script>
    Start in <span id="shownum">9</span> seconds....
    Peace,
    Last edited by Barti1987; 11-26-2007 at 09:30 PM.
    Testing 1.. Testing 1..2.. Testing 1..2..3...

  8. #8
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    ohhhh nice one

    I wanted to acually do the count down timer without the use of form + input box. Thanks allot azizny

    --

    EDIT :: Is it possible to have 2 span <span id="seconds">30</span>.<span id="millisecond">9</span> amd in the script make the millisecond go from 9 to 0 in a loop?

    So it looks something like this:

    30.9
    30.8
    30.7
    30.6
    30.5
    30.4
    30.3
    30.2
    30.1
    30.0
    29.9
    29.8

    ... and so on?
    Last edited by latheesan; 11-27-2007 at 01:02 AM.

  9. #9
    yes, but every span must have own js function one for the seconds and one for the milliseconds

  10. #10
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    This is how i tried to do it:

    HTML Code:
    function cd_sec() {
    	x = parseInt(document.getElementById('seconds').innerHTML);
    	x--;
    	document.getElementById('seconds').innerHTML = x;
    	if (x > 0){
    		setTimeout("cd_sec()",1000);
    	} else {
    		loadcaptcha();
    	}
    }
    
    function cd_millisec() {
    	y = parseInt(document.getElementById('milliseconds').innerHTML);
    	y--;
    	document.getElementById('milliseconds').innerHTML = y;
    	if (y > 0){
    		setTimeout("cd_millisec()",100);
    	} else {
    		y = 9;
    	}
    }
    the seconds are counting down properly, however, the milliseconds goes from 9 to 0 and then it doesnt do anything, it stays 0.

    How do u make it go in a loop?

  11. #11
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,849
    Look at your conditional:
    HTML Code:
    	if (y > 0){
    		setTimeout("cd_millisec()",100);
    	} else {
    		y = 9;
    	}
    }
    What else is it going to do but count down to zero and stop?

    For this sort of application you'd be better off using setInterval anyway, something like this (untested):
    Code:
    var sCounter = window.setInterval("cd_sec()",1000);
    var msCounter = window.setInterval("cd_millisec()",100);
    function cd_sec() {
    	x = parseInt(document.getElementById('seconds').innerHTML);
    	x--;
    	document.getElementById('seconds').innerHTML = x;
    	if (x == 0){
    		window.clearInterval(sCounter);
    		window.clearInterval(msCounter);
    		loadcaptcha();
    	}
    }
    
    function cd_millisec() {
    	y = parseInt(document.getElementById('milliseconds').innerHTML);
    	y--;
    	if (y < 0){
    		y = 9;
    	}
    	document.getElementById('milliseconds').innerHTML = y;
    }
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  12. #12
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    764
    Thanks allot, your suggestion works great

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •