-3
    <html>
    <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>
    <body>
    <script> 


  var timer = document.getElementById("logout-timer")  
    , now = new Date()  
    , deadline = new Date(now.getFullYear, now.getMonth, now.getDate, now.getHours, now.getMinutes + 15);  

  timer.innerHTML = countdown(deadline).toString();  
  setInterval(function(){  
    timer.innerHTML = countdown(deadline ).toString();  
  }, 1000);  


    </script> 

    You will be logged out in <span id="logout-timer"></span>  
    </body>
    </html>

I have this script that works on fiddle.js but when i implement it into my website ^ like that it doesnt work why?

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
mpfasobd
  • 1
  • 1

1 Answers1

3

Move the script to after the span, or wrap the code in a function and call it when the ready or load event fires.

document.getElementById("logout-timer") can't find the span before it exists.


You also need to define countdown; there is no sign of it in your code.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • Tried that, no change – mpfasobd Jul 12 '14 at 19:46
  • 2
    This is almost always the reason for something working in a fiddle (which defaults -- bizarrely -- to using onload) and not working otherwise. – T.J. Crowder Jul 12 '14 at 19:46
  • Why does this work on fiddle, and why the deleted answer was wrong? – Vlas Bashynskyi Jul 12 '14 at 19:47
  • 2
    @VLAS: It's perfectly valid to leave off the protocol. It's a relative URL, relative to the protocol used to load the page. More: http://blog.niftysnippets.org/2011/01/skipping-protocol.html – T.J. Crowder Jul 12 '14 at 19:49
  • 1
    @VLAS — It works on the fiddle because, as TJ mentioned, it wraps the JS in a function and calls it on load. The deleted answer was wrong because (a) it failed to fix the problem described in this answer and (b) made some assumptions (that your HTML document was being loaded using a `file://` URI) that the subject of your question showed were wrong. – Quentin Jul 12 '14 at 19:49