Javascript计时器问题

时间:2011-08-21 13:18:52

标签: javascript html timer

我的计时器不会显示任何内容,我认为它与insertBefore函数有关,但我不确定。关于为什么它在我的范围内没有显示任何数字的任何想法?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function timeMsg()
{
var t=setTimeout("displayMsg()",5000);
}
function displayMsg()
{
document.getElementById("timeoutmsg").style.display = 'block';
}

COUNTER_START = 60

function tick () {
    if (document.getElementById ('counter').firstChild.data > 0) {
        document.getElementById ('counter').firstChild.data = document.getElementById ('counter').firstChild.data - 1
        setTimeout ('tick()', 1000)
    } else {
        document.getElementById ('counter').firstChild.data = 'done'
    }
}

if (document.getElementById) onload = function () {
    var t = document.createTextNode (COUNTER_START)
    var p = document.createElement ('P')
    p.appendChild (t)
    p.setAttribute ('id', 'counter')

    var body = document.getElementsByTagName ('timer')[0]
    var firstChild = body.getElementsByTagName ('*')[0]

    body.insertBefore (p, firstChild)
    tick()
}

</script>
</head>

<body>
<div id="timeoutmsg" style="border: solid 2px black; background-color: #69F; width: 300px; height: 100px; text-align: center; position: absolute; display: ; left: 40%; top: 40%; z-index: 1;" >Your Session will expire in  <span id="timer"></span> Click Here to Contine or you will be logged out.</div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

试试这个http://jsfiddle.net/d4ft4/

问题出在这一行

var body = document.getElementsByTagName ('timer')[0]

将其更改为

var body = document.getElementById('timer');

做了这个伎俩