Countdown Timer

Discussion in 'JavaScript' started by DennyLoi, Nov 4, 2007.

  1. #1
    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?
     
    DennyLoi, Nov 4, 2007 IP
  2. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #2
    So, the code worked on a standalone page, but when you put it on a new page, that's when you started getting the error? In my experience, that's usually caused by a JS or validation error somewhere else on the page where you've placed the code.

    Can you post a link to the code on the page where you're having trouble? It may be all about the context in this case.
     
    KatieK, Nov 5, 2007 IP