Wrox Programmer Forums
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 4th, 2007, 02:25 PM
Registered User
Join Date: Nov 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Countdown timer

Hi everyone.

I am trying to implement a countdown timer which is displayed on my page. The counter needs to countdown from 10 seconds to 0 upon the page loading up.

I tried the following, but am running into difficulties.

//Some other script here^^^^

var counterForm = document.createElement("form");
    counterForm.name = "form";
      counterForm.action = "";
       var para5 = document.createElement("p");
       var counterInput = document.createElement("input");
    counterInput.type = "text";
      counterInput.name = "abc";
      counterInput.id = "abc";
      counterInput.value = '';

    } //closes this function

    var milisec=0 //simple variables
    var seconds=10
    function display(){
    if (milisec<=0){
    if (seconds<=-1){
    else {

The first part of the code is I snippet of function start() which is called as follows:

<body onload="start()">

The error I’m being given is “counterInput is not defined”.

I had this script working as a separate page, but when I tried adding into my work, the problems started.

Any suggestions?

Old November 26th, 2007, 01:42 PM
Friend of Wrox
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts

1) There are several semicolons missing from your code.
2) Try using:
   document.getElementById("abc").value = seconds + "." + milisec;
   document.form.abc.value = seconds + "." + milisec;
   Instead of:

Hope this helps.
Old November 27th, 2007, 08:01 AM
Friend of Wrox
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

Yeh, counterInput is only in scope in yer start() function. You created a variable counterInput which points to a document element named "input" in that function when you said:

var counterInput = document.createElement("input");

So you could just give it the name you wanted:

var counterInput = document.createElement("counterInput");
Or call it with the name input thus:

document.getElementById("input").value = seconds + "." + milisec;

Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk

Similar Threads
Thread Thread Starter Forum Replies Last Post
Countdown Timer stalkingbutler Access VBA 2 April 16th, 2007 10:44 AM
Countdown timer ashik112 Classic ASP Basics 1 February 25th, 2007 07:09 PM
How to Create a countdown timer ashik112 ASP.NET 1.0 and 1.1 Basics 1 February 14th, 2007 10:30 AM
Countdown Timer loveperson Beginning VB 6 1 January 7th, 2005 05:31 PM
ebay countdown timer!! Ashleek007 PHP How-To 1 April 28th, 2004 01:44 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.