tags:

views:

31

answers:

2

I have two numeric fields that contain the following information:

(1) Users Registered (2) Money Raised

Is it possible to make the data change in real time (EVERY SECOND) while someone is viewing the page with the fields? I don't want the viewer to refresh manually to see the updated number, but have the fields change automically while the page is viewed. I want it to be similar to like a text clock that actually shows the seconds counting, etc.

The fields are connected to a database that is constantly changing every second.

Does anyone know if this is possible? Any examples? or Suggestions?

I really appreciate it.

Erik

+1  A: 

You have 2 options:

You can use the setTimeout feature to make an ajax request every second and update the web page.

If you can limit your self to the most current web browsers, you could use WebSocket to maintain an persistant connection to the server, allowing the server to send out updates whenever necessary.

EDIT

There is also Comet, but that might be overkill.

mikerobi
Will the socket option work with IE7?
Erik
Will the viewer notice a flicker or slow performance with the setTimeout feature?
Erik
Definitely not, but there is a workaround using flash (I can't vouch for its quality): http://github.com/gimite/web-socket-js
mikerobi
It seems the setTimeout would be the best approach? Are there any drawbacks?
Erik
@Erik, the drawbacks are that you can't get continues updates, you can only get an update when you check for it, the advantage is that it is universally supported. The setTimeout approach also requires creating a new network connection for each update, so there is increased latency for.
mikerobi
A: 

I guess this is a web application. In this case you would make a request to your server with XMLHttpRequest every second. The field can then be updated with the response. You might also use websockets which open a permanent connection to the server. Unfortunately they are only supported by newer browsers.

An example for my first proposal with jQuery:

// This function starts the request and calls success on response.
var refresh = function() {
  $.ajax({
    url: "/some/path",
    cache: false,
    success: success
  });
}

// Replaces the contents of the field with your response and
// triggers refresh() after 1000ms.
var success = function(data) {
  $(".field").html(data);
  setTimeout(refresh, 1000);
}

// Starts processing when document is ready.
$(function() {
  refresh();
}
balu
Would this work in IE7?
Erik
WIll the viewer experience in delay or slow performance of the web pages?
Erik
This will work in IE7. Timing will be:Page load time + Request time => First update of the field;1000ms + Request time; => Second update of the field
balu
There will be no flickering of the page. It just will not update EXACTLY each second as you need to do a request between each update.With websockets this would change, as the server would just spit out a new value to the client every second which could then update immediately.If you're new to web applications, I would still recommend to go with AJAX as websockets are kind of bleeding-edge and will need more work and a more difficult server-side application.
balu