Saturday, October 27, 2007

Ajax clock, an alive clock using the server time

In this tutorial we will see how easy is to develop a simple clock using the prototype Ajax classes, specifically Ajax.PeriodicalUpdater class.

You can download this tutorial files here.

Developing a JavaScript clock is not so difficult, and there are a bunch of them available in the net. But sometimes we need to show the server's time on the clock, instead of using the clients' own machine time. So the solution is Ajax if we want to have an interactive (alive) clock on the page which is showing the server's time without refreshing the page again and again.


1.
Create a html file and write the following code :

in the head section:



in body section:




2.
Create a PHP file called clock.php containing the following code:




3.
Create a floder called js besides these files and copy the prototype.js in it. You can download the latest version of prototype framework here.

Thats it! Copy all the files in the apache htdocs folder and enjoy!


Explanation:

We have a div with id="clock" in the html body which is our placeholder for displaying the time.
In the JavaScript section we have created a new object of class Ajax.PeriodicalUpdater which is one of the Ajax classes provided by prototype framework.
According to the prototype document:

This object addresses the common need of periodical update, which is used by all sorts of “polling” mechanisms (e.g. in an online chatroom or an online mail client).

So we don't need to provide a mechanism for fetching the time again and again, this object will repeat its job automatically based on its frequency option in the options parameter.
The first parameter is the id of the HTML element which will show the result and the second one is the URL for the request (XMLHttpRequest).

Enjoy Ajax!


13 comments:

Macielo said...

Wow, that IS easy. However I can't find why is not working in IE7 but it's doing perfectly in Firefox. By the way, I'm using WAMP server for testing purposes...Any Ideas?

Macielo said...

>>"However I can't find why is not working in IE7 but it's doing perfectly in Firefox"

Nevermind, I just changed the method to 'post' and now it works perfect on firefox AND IE!

Anonymous said...

Tried to post a comment to fix the ajax problem in IE7 the problem is this form wont aloow php scripts to be posted the answer is to change the headers to make it work in IE7 so if you need the answe fill in the contact form on www.directelitewebsites.co.uk and i will post you the script

Seven said...

>>"However I can't find why is not working in IE7 but it's doing perfectly in Firefox"

Sorry for delay!
What is the problem in IE? Does it crash or something else?
As far as I know Prototype is compatible with IE but some broken IE versions crash when loading Prototype.

Anonymous said...

IT is Wonderful but Wile the Time is Going to be changed stats bar is active and the USER think that The page have not been Loaded and some data have been missed!

Is there any way that we first set the clock with SERVER o'clock and it start in a browser based script?


thanks For this method.

Best Regards

Jack said...

Hi there... I found a website that is www.lozix.org this web portal has complete solution of software, web developement and SEO etc...

http://www.lozix.org

Anonymous said...

How would I go about gradding the time stamp value?

Anonymous said...

grabbing. I meant grabbing. Thanks! I guess I should clarify. I want to put the displayed clock value(time) into a PHP var so I can dump it into a database.

Seven said...

What do you want to do exactly?
Doesn't it work for you?

Seven said...

aha, you can have an int field in your db and insert this value there:

$currentTimeStamp = time();

Take a look here:
http://php.net/manual/en/function.time.php

Unknown said...
This comment has been removed by a blog administrator.
Unknown said...

I cannot download the files...

Seven said...

You are right, the 4shared account has expired. But you can copy the codes and paste them in new files with the names mentioned in the tutorial.