NetTalk Central

Author Topic: Adding JavaScript To A Timer  (Read 11044 times)

lanmicro

  • Full Member
  • ***
  • Posts: 112
    • View Profile
    • Email
Adding JavaScript To A Timer
« on: June 24, 2010, 05:01:53 PM »
Hi Guys,

Someone here posted a javascript that puts a countdown until the session ends.  Here it is.  How do I implement this into my NetTalk project?

<form name="counter">
    <p align="center">
    <input type="text" size="82" name="d2" style="font-weight: bold; border: 3px solid #FF0000; color:#FF0000; background-color:#FFFF00; text-align:center">
</form>
<script>
<!--
var seconds=60
var minutes = 1
document.counter.d2.value="If you do not work you will automatically be logged out in "+minutes+" minutes and "+seconds+" seconds."
function display(){
if (seconds<=0){
    seconds=60
    minutes-=1
}
if (minutes<0){
    seconds=0
    minutes=0
    window.open('http://www.dynamicdrive.com/forums/showthread.php?t=6138','_top');
}
else
    seconds-=1
    document.counter.d2.value="If you do not work you will automatically be logged out in "+minutes+" minutes and "+seconds+" seconds."
    setTimeout("display()",1000)
}
display()
-->
</script>


 
Gregory C. Bailey

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Adding JavaScript To A Timer
« Reply #1 on: June 25, 2010, 02:53:55 AM »
You can drop this, pretty much "as is" in your Footer tag.
that way it's included on all pages where your footer appears.

You might want to fiddle with the style a bit (the style of the input control) and of course set the "window.open" to your front page - ie something like
  window.open('indexpage','_top');

lanmicro

  • Full Member
  • ***
  • Posts: 112
    • View Profile
    • Email
Re: Adding JavaScript To A Timer
« Reply #2 on: June 26, 2010, 12:45:45 PM »
Thanks Bruce, works a treat!
Gregory C. Bailey

lanmicro

  • Full Member
  • ***
  • Posts: 112
    • View Profile
    • Email
Re: Adding JavaScript To A Timer
« Reply #3 on: June 30, 2010, 07:44:34 PM »
How can I get my script to refresh if I never leave the page?  I would like the countdown to restart if I enter something into an entry field that causes the browse to refresh but the script thinks that I am on the same page and the countdown continues.
Gregory C. Bailey

bshields

  • Sr. Member
  • ****
  • Posts: 392
    • View Profile
    • Inhabit
    • Email
Re: Adding JavaScript To A Timer
« Reply #4 on: July 01, 2010, 04:14:47 AM »
Hi,

You could add another function directly after the "display" function to set the timer.

function settimer(){
  minutes=1
  seconds=60
}


This will set the variables back to the timeout value (you could just access the javascript variables directly, but this approach is more polite).

The real problem is how do you call this function each time NT does an ajax request. You could add some javascript to everyplace NT does a ajax call. But thats a real pain. Another approach is to edit NT's sv function to add support for this idea (and possibly other functions). This has its own problems, each time NT is updated you'd have to update it with your "changes". You'd also have to promote your variables and functions into NTs javascript so they are in scope.

This approach is certainly possible - but painful, another idea would be to ask Bruce nicely to do it for you :)

Regards
Bill