0

I'm following a tutorial about creating a timer in JavaScript but I can't get it to show up on my browser. I'm using Dreamweaver and it shows the timer just fine in a "live view" but in the browser window nothing is being displayed. What am I missing?

This is the main HTML page:

<html>
    <head>
        <script type="text/javascript" src="/Timer2.js" />
    </head>
    <body>
        <div id='timer' />
        <script type="text/javascript">
            window.onload = CreateTimer("timer", 90);
        </script>
    </head>
    </body>
</html>

This is the JavaScript timer function. timer2.js:

var Timer;
var TotalSeconds;

function CreateTimer(TimerID, Time) {
    Timer = document.getElementById(TimerID);
    TotalSeconds = Time;

    UpdateTimer()
    window.setTimeout("Tick()", 1000);
}

function Tick() {
    if (TotalSeconds <= 0) {
        alert("Time's up!")
        return;
    }

    TotalSeconds -= 1;
    UpdateTimer()
    window.setTimeout("Tick()", 1000);
}

function UpdateTimer() {
    Timer.innerHTML = TotalSeconds;
}

function UpdateTimer() {
    var Seconds = TotalSeconds;

    var Days = Math.floor(Seconds / 86400);
    Seconds -= Days * 86400;

    var Hours = Math.floor(Seconds / 3600);
    Seconds -= Hours * (3600);

    var Minutes = Math.floor(Seconds / 60);
    Seconds -= Minutes * (60);

    var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds)

    Timer.innerHTML = TimeStr ;
}


function LeadingZero(Time) {
    return (Time < 10) ? "0" + Time : + Time;
}
2
  • 1
    first <div id='timer' /> should be <div id='timer' ></div> Commented Aug 8, 2012 at 23:00
  • 1
    Are you using your browser's console to see what errors your script is genrating? Commented Aug 8, 2012 at 23:19

2 Answers 2

4

You can't self-close a script tag. You MUST write <script ....></script>

Sign up to request clarification or add additional context in comments.

7 Comments

yes it is self-closed, but you better don't do that. See stackoverflow.com/questions/4531772/…
Ok I changed it but this still does not fix the display problem in the browser.
Well there's at least 10 different things wrong with your code. Did you write that, and if so who taught you?
Well the person who wrote that is a dunce and should not be listened to. Never EVER put a string in setTimeout. ALWAYS put a semicolon at the end of a "line". AVOID global variables. Don't rely on the timer being accurate (use delta timing where possible). Identifiers stating with an uppercase letter should be reserved for instanciable object names. You are defining the same function twice. You are using innerHTML when nodeValue (on a text node in the element) would be more appropriate. You are repeatedly calling setTimeout to simulate what setInterval does for you. And so on...
|
1

Write <div id="timer"></div> and no <div id='timer' />

Delete your </head> at the bottom of the code, and if your script doesn't work again, try src="Timer2.js" without the "/"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.