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

4

3 回答 3

2

您的小提琴加载 MooTools 但您的页面没有。

改变:

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

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

并使用纯 JS。

jsFiddle 示例

于 2013-09-13T14:13:07.447 回答
1

Number没有办法floorMath改为使用

Math.floor( time / 60 );
于 2013-09-13T14:18:29.723 回答
0

您的 jsFiddle 加载 MooTools,它使用 floor 方法扩展本机 Number 对象。

您的代码没有,因此您的代码在尝试调用 Number.floor 时会出错。请改用 Math.floor。

好吧,在它被定义之后调用 countDown 。

于 2013-09-13T14:18:31.797 回答