Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old November 4th, 2007, 02:25 PM
Registered User
 
Join Date: Nov 2007
Location: , , .
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 = '';
      counterForm.appendChild(counterInput);
    para5.appendChild(counterForm);
      document.body.appendChild(para5);

    } //closes this function

    var milisec=0 //simple variables
    var seconds=10
    function display(){
    if (milisec<=0){
    milisec=9
    seconds-=1
    }
    if (seconds<=-1){
    milisec=0
    seconds+=1
    }
    else {
    milisec-=1
    document.counterInput.value=seconds+"."+milisec
    setTimeout("display()",100)
    }

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

<body onload="start()">
</body>

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?



  #2 (permalink)  
Old November 26th, 2007, 01:42 PM
Friend of Wrox
Points: 513, Level: 8
Points: 513, Level: 8 Points: 513, Level: 8 Points: 513, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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

Hope this helps.
  #3 (permalink)  
Old November 27th, 2007, 08:01 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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:

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

So you could just give it the name you wanted:

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

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




--
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.