Simple Ajax

J

Jorge

With setTimeout you poll the server every 2 seconds + server response
lag (0, 2+[time to response 1], 4+[time to response 2], and so on).

No Diego, not necessarily.
- 1st client request:  0,00 (start point)
- 1st server response: 1,87 seconds (then wait 2,00 seconds)
- 2nd client request   3,87 seconds (result of 1,87 + 2,00 of timeout)
- 2nd server response: 1,99 seconds (then wait 2,00 seconds)
- 3rd client request:  7,86 seconds (result of 3,87 +1,99 +2,00
seconds)
- 3rd server response: 3,00 seconds (then wait 2,00 seconds)
- 4th client request:  12,86 seconds (result of 7,86 +3,00 +2,00
seconds)
... and so on!

Test case with my alghoritm:

- 1st client request: 2,00 (start point)
- 1st server response: 1,87 seconds
- 2nd client request: 4,00
- 2nd server response: 1,99 seconds
- 3rd client request:  6,00
- 3rd server response: 3,00 seconds
- 4th client request: 10,00
... and so on!

You can do that as well with a setTimeout.
In general it's usually better to use a self-re-setting setTimeout
than a setInterval.
 
D

Diego La Monica

With setTimeout you poll the server every 2 seconds + server response
lag (0, 2+[time to response 1], 4+[time to response 2], and so on).

No Diego, not necessarily.

Why not? If you see the Thomas's alghoritm behavior is as described
(above).
You can do that as well with a setTimeout.
Agree.

In general it's usually better to use a self-re-setting setTimeout
than a setInterval.

Jorge, I understand clear like the water what you write, maybe that
Thomas would say me the same, maybe. I will document about it, but if
you have some links that explain accuratelly why, I'll apreciate them
a lot!

Cheers!
Diego La Monica
 
T

Thomas 'PointedEars' Lahn

Diego said:
Please, let us see your test cases.

As I said, it was/is production code where I observed that difference, so a
trimmed-down test case cannot reproduce the exact conditions that led to my
observations, and I cannot disclose the entire relevant code (which is
partially server-side) or the location of it.

Suffice it to say that the difference between "self-calling"
window.setTimeout() and window.setInterval() appears to be more significant
the more is done in each step (especially the number of DOM operations
matters), the more things should happen seemingly in parallel, and the
faster this should happen.

In my production code I had two areas where several positioned block
elements were moved in order to emulate a scrolling band of items each, one
horizontally from right to left, one vertically from bottom to top. Showing
one element and hiding another forced a request to an image resource as the
`src' property of the DOM object that represented a moving, showed `img'
element was modified accordingly.

I am sure you can build a test case for yourself based on this, although
common sense should suffice.


PointedEars
 
D

Diego La Monica

Please, let us see your test cases.

As I said, it was/is production code where I observed that difference, [....]
I am sure you can build a test case for yourself based on this, although
common sense should suffice.

I don't like to recreate the wheel.. You told me that your code with
setTimeout is better in your production environment, but that not
means that is always better than setInterval.
Because I like to learn always more than I know, I will post you 2
links: 1 article and 1 example.

The 1st link[1] describe the difference between setTimeout and
setInterval. As I can read there, setInterval "is a function that can
execute other javascript statement EVERY x interval. The interval unit
is millisecond."
Bryan asked us to do something every 2 seconds, not AFTER 2 seconds.
But leave it.
The 2nd link[2] will show you an example of using setInterval against
setTimeout. Take a look to the results.
I'm considering those information quite reliable. Don't you?

With regards.
Diego La Monica

[1] http://rapd.wordpress.com/2007/10/11/javascript-settimeout-vs-setinterval/
[2] http://dhtmlkitchen.com/learn/js/perf/animation/setInterval.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,797
Messages
2,569,646
Members
45,374
Latest member
VernitaBer

Latest Threads

Top