1.11M Members

Increasing counter with variable speed in Ajax

 
0
 

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.

LastMitch
Deleted Member
 
0
 

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.

 
0
 

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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: