Results 1 to 12 of 12
-
11-26-2007, 02:14 PM #1Web Hosting Master
- 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>
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);
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:: www.AsuraHosting.com ::
-
11-26-2007, 02:41 PM #2Web Hosting Evangelist
- 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);
-
11-26-2007, 02:41 PM #3Web Hosting Master
- Join Date
- Jan 2006
- Location
- Athens, Greece
- Posts
- 1,481
<form name="dw" id="dw">
-
11-26-2007, 02:59 PM #4Web Hosting Master
- 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:: www.AsuraHosting.com ::
-
11-26-2007, 03:06 PM #5Web Hosting Master
- Join Date
- Jan 2006
- Location
- Athens, Greece
- Posts
- 1,481
Add an id to the input text tag.
-
11-26-2007, 04:20 PM #6Web Hosting Master
- Join Date
- Jan 2005
- Location
- UK, London
- Posts
- 764
Aww nice one, thanks for that, script works great
:: www.AsuraHosting.com ::
-
11-26-2007, 09:23 PM #7Web Hosting Master
- 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....
Last edited by Barti1987; 11-26-2007 at 09:30 PM.
Testing 1.. Testing 1..2.. Testing 1..2..3...
-
11-27-2007, 12:56 AM #8Web Hosting Master
- 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.
:: www.AsuraHosting.com ::
-
11-27-2007, 10:20 AM #9Disabled
- Join Date
- Aug 2007
- Posts
- 292
yes, but every span must have own js function one for the seconds and one for the milliseconds
-
12-03-2007, 01:08 AM #10Web Hosting Master
- 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; } }
How do u make it go in a loop?:: www.AsuraHosting.com ::
-
12-03-2007, 01:30 AM #11Retired Moderator
- 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; } }
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-03-2007, 03:03 AM #12Web Hosting Master
- Join Date
- Jan 2005
- Location
- UK, London
- Posts
- 764
Thanks allot, your suggestion works great
:: www.AsuraHosting.com ::