-1

My problem is thesame as the following:

Very simple javascript doesn't work at all

But in my case the answers dont help.

In JS fiddles it does work, but when I copy the source code of the JS fiddle result frame it doesnt work anymore (on my server/ computer).

My code (simplified):

<!DOCTYPE html>
<html>
<head>
    <title>Stack Overflow</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
    <p id="countdown2"></p>

    <script>

    function countdown(element, minutes, seconds) {
    var time = minutes*60 + seconds;
    var interval = setInterval(function() {
        var el = document.getElementById("countdown2");
        if(time == 0) {
            el.innerHTML = "countdown's over!";    
            clearInterval(interval);
            return;
        }
        var minutes = Number.floor( time / 60 );
        if (minutes < 10) minutes = "0" + minutes;
        var seconds = time % 60;
        if (seconds < 10) seconds = "0" + seconds; 
        var text = minutes + ':' + seconds;
        el.innerHTML = text;
        time--;
    }, 1000);
    }
    countdown("countdown2", 9, 23);

    </script>
</body>
</html>

In this JS fiddle it works though. http://jsfiddle.net/Apnu2/374/

My simplified online page:

http://biefstuk.org/mc/faal.html

I've been working on this for more than an hour now...

Community
  • 1
  • 1
Carrot
  • 188
  • 1
  • 1
  • 8

3 Answers3

2

Your fiddle loads MooTools but your page doesn't.

Change:

var minutes = Number.floor( time / 60 );

to

var minutes = Math.floor( time / 60 );

and use plain JS.

jsFiddle example

j08691
  • 190,436
  • 28
  • 232
  • 252
1

Number has no method floor. Use Math instead

Math.floor( time / 60 );
Ilya
  • 27,538
  • 18
  • 104
  • 148
0

Your jsFiddle loads MooTools, which extends the native Number object with a floor method.

Your code does not, hence your code runs into an error when it tries to call Number.floor. Use Math.floor instead.

Well, and call countDown after it was defined.

hagbard
  • 665
  • 3
  • 13
  • countdown *is* defined at the top of the scope. There is nothing wrong with invoking it at the very top. – meager Sep 13 '13 at 14:20
  • True - if it would have been in the same script tag (as it is now). But since script tags are executed in order of appearance and his original code had script tag 1: calling the function, script tag 2: defining it - at the time that was valid advice :) – hagbard Sep 13 '13 at 14:29
  • The invocation and definition have *always* been in the same script tag, since the original version posted. His code has only ever contained a single script tag. – meager Sep 13 '13 at 14:30
  • Huh.. the version history says so, in that case you're right. :) – hagbard Sep 13 '13 at 14:36