2011-08-21 5 views
-1

Mon minuteur n'affichera rien, je pense que cela a quelque chose à voir avec la fonction insertBefore mais je ne suis pas sûr. Des idées sur pourquoi il n'affiche aucun nombre dans mon span?Javascript Timer Problème

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

Répondre

2

essayer cette http://jsfiddle.net/d4ft4/

le problème était à cette ligne

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

changer à

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

a fait l'affaire