开发者

Javascript Timer Problem

My timer wont display anything, I think it has something to do with the insertBefore function but I am not sure. Any ideas on why it doesn't display any numbers in my span?

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


try this http://jsfiddle.net/d4ft4/

the problem was at this line

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

changing it to

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

did the trick

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜