• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Timer on WebPage

How can display or run a timer with current date and time?without refreshing the page the should run?
0
dharan_p
Asked:
dharan_p
  • 6
  • 3
  • 2
  • +1
1 Solution
 
jkunalCommented:
not possible.
0
 
makerpCommented:
use javascript on the client .. this is cool. you could use a div or layer to display the time if you wish

<HTML>
<HEAD>
<TITLE> Date and Time </TITLE>
</HEAD>
<head>

<script>
// Global variables for timer.
var timerID = null;
var timerRunning = false;
var timevalue;

function startclock ()
   {
   // Make sure the clock is stopped
   stopclock();
   time();
   }

function stopclock ()
   {
   if(timerRunning)
      clearTimeout(timerID);
   timerRunning = false;
   }

function time ()
   {
   var now = new Date();
   var ampm = (now.getHours() >= 12) ? " P.M." : " A.M."
   var hours = now.getHours();
   hours = ((hours > 12) ? hours - 12 : hours);
   var minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();
   var seconds = ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();

   timevalue =(" " + hours + minutes + seconds + " " + ampm);
   // displays the current time.
   document.forms[0].local.value = timevalue;

   timerid = setTimeout("time()",1000);
   timerrunning = true;
   }
document.write(timevalue);
</script>
</head>
<body onload="startclock ()">
</center>
        <script>
       
        var now = new Date()
        var sec = now.getSeconds()
   
function MakeArray(n) {
        this.length = n;
        for (var i = 1; i <= n; i++) {
                this[i] = 0 }
            return this        
     }
     // end script -->
      </script>
   
<form>
<table width=180><tr>
<td>
 <input type="text" name="local" size=12 value=""></td><td align=left>
</td></tr></table><p>
</form>

<LAYER id=timelayer>
      
</LAYER>

</body>
</html>

</BODY>
</HTML>
0
 
frueyCommented:
This is possible, but not using just ASP

Using Javascript you can get the current users' date and time, that is to say the *local* time for that user.

If their system clock is wrong, it looks bad on your own code, which is why it's not often a good option.

However, I did get something working where the time is set by ASP and then sent to Javascript, parsed, and then "javascript time" can be correct whatever the users' settings are.

Structure of the code:

Get the time via ASP
- this will be your server's system time

Parse this into a Javascript new date() variable (as the ASP time returned is a text string)

Set up a form element (usually a text input box) which can be updated dynamically via Javascript

Set a timer in javascript to update it each minute / second / whatever

Note that the time will always be your server's local time, and therefore your server has to have the correct time :)

You want to see some examples of code for all that? hmmmm

I developed a (non dynamic for the moment) code that also shows internet time (@time) and local times in several timezones, plus local time (users system) using a combination of ASP and Javascript. But I'd be reluctant to just give it away for 100 points, especially not to everyone. Persuade me.
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
makerpCommented:
that code i posted .. im just tidying it up and getting it to diplay date aswell
0
 
BaneBaneCommented:
Well actually it is possible, you can use javascript.

what you might want to do is create a <div> element give it an ID. say <div id="d1">.

using javascript code, which you can start at the on_load, use the method setTimeout(SOME_FUNCTION,TIME_IN_Msec_TO_REFRESH);

this method will go to sleep and wake up at the specified time to run a function.

the function called should change the innerHTML of the div element to show the new time.

changing the innerHTML does not cause thepage to reload and so you get a clock.

Hope this helps.
0
 
frueyCommented:
Makerp again quick on the draw :-)

Using makerp's script you've got the javascript part covered... Just use a bit of ASP to make the time correct (and not just local time) and you're guaranteed to be accurate (my webserver is accurate to drift of less than 1/10 second per year)...

BTW makerp - what does the function MakeArray do? You don't call it anywhere...

0
 
makerpCommented:
a bit tydier ... :)

<HTML>
<head>

<script>
// Global variables for timer.
var timerID = null;
var timerRunning = false;
var timevalue;

function startclock ()
   {
   // Make sure the clock is stopped
   stopclock();
   time();
   }

function stopclock ()
   {
   if(timerRunning)
      clearTimeout(timerID);
   timerRunning = false;
   }

function time ()
   {
   var now = new Date();
   var ampm = (now.getHours() >= 12) ? " P.M." : " A.M."
   var hours = now.getHours();
   hours = ((hours > 12) ? hours - 12 : hours);
   var minutes = ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();
   var seconds = ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();

   timevalue = now.getDate() + "/" + (now.getMonth() + 1) + "/" + now.getYear() + " " + hours + minutes + seconds + " " + ampm;
   // displays the current time.
   document.forms[0].local.value = timevalue;
   timerid = setTimeout("time()",1000);
   timerrunning = true;
   }
</script>
</head>
<body onload="startclock ()">
       
<form>
 <input type="text" name="local" size=30 value="">
</form>
0
 
makerpCommented:
its a scrappy bit of code in my first post ..

MakeArray serves no purpose what so ever
0
 
makerpCommented:
you best using just javascript to get the time as then your page will be independant and will show the correct time where ever in the world as it will be taken from the client not server

:)
0
 
makerpCommented:
getting the time in asp will be no good as it will be incorrecct by the time it reaches the client.

asp inserts the current time into a javascript var.
(its then a static time)

it then spends 30 secs getting to the client over the net..

wrong time.

if the client cannt set there clock on there pc right then they shouldnt have one and maybe should go out and buy an 'tomy , my first computer' complete with 65 spelling excercises and hang man
0
 
jkunalCommented:
I am sorry i should have said not possible using asp.
0
 
frueyCommented:
Yep makerp, you're right. By the time it gets to the client it's a few seconds out, but

1) IT's better than the average users' clock in my experience, especially where I live (Morocco)

2) You can get the timezone offset from Javascript and change your ASP time accordingly, or calculate the time offset between your server and your target audience and label your time "New York" or "Casablanca"

but generally I agree.. As long as you're happy that users can set their clock right. But did you know the average clock drift of a cheap PC clock chip can be up to several seconds A DAY which means even with transmission delays the time served by ASP (given that my server time is definitely accurate) will be better...

:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now