Hi everyone.

I would like to implement a counter using ajax that will act like this:
The script will send a XMLHttpRequest every x seconds which will return a value from a mysql database.
Using the current and previous value retrieved from the server, the script will increase the counter, in order to reach the new value in the x second interval between the server requests.

The value will be decimal (two decimal digits are enough), it will always icrease (until it resets) and my main concern is to make it increase with variable speed, to give a more interesting visual effect. Meaning that if the (current value - previous value) is big the counter will speed up to "catch up".

I already have code that reads a value from a database and just prints it to a html div element every x seconds.
So i would like a suggestion on the counter increase algorithm.

Hope i made it clear... :)

Thanks in advance.

Recommended Answers

All 2 Replies

Member Avatar for LastMitch

I would like to implement a counter using ajax that will act like this:
The script will send a XMLHttpRequest every x seconds which will return a value from a mysql database.
Using the current and previous value retrieved from the server, the script will increase the counter, >in order to reach the new value in the x second interval between the server requests.

What code have you done so far? A little code provided would be helpful at least there's a direction.

Sorry i didn't post any code, i was in a hurry and wanted a suggestion because i was stuck.
In the end, i worked it over last night and managed to do it.

The key was to use setTimeout function with a variable timeout parameter. Timeout should be calculated on each call, using the previous and current value, and then passed to the next setTimeout call.

Thanks anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.