views:

53

answers:

2

I am writing an AJAX script that basically calls a PHP page and requests some information. The PHP page queries a database. I want to set the page to make the call every 5 minutes, but the only way I know how to do so is by using the settimeout function. I am wondering if this settimeout function is constantly running is this going to be harsh on the page's performance? is there another way to go about this?

+1  A: 

use setInterval. They should render the same results (~0 performance loss), but setInterval is the obvious way to go.

Think about Google Wave: they send an AJAX request for every letter you type, yet it doesn't feel slow at all, so one request every 5 minutes is nothing.

Gabi Purcaru
thankyou very much.
mcbeav
GMail as well to check for new messages. And think of GMail chat. Even more frequently.
Robert Koritnik
I fully disagree, `setInterval` is (mostly) evil. If your code would, for example break, it would just stack up and never end. Read this to understand why you should (almost) always use `setTimeout` over `setInterval`: http://zetafleet.com/blog/why-i-consider-setinterval-harmful
peol
A: 

I don't really thing the problem is setTimeout, perhaps it is the way you have built the script.

In fact, most of the time setTimeout it's preferred, for a reason, setTimeout will wait the execution of your script, setInterval will execute in every interval without waiting.

Pls look here at the bottom:

https://developer.mozilla.org/En/Window.setInterval

If there is a possibility that your logic could take longer to execute than the interval time, it is recommended that you recursively call a named function using window.setTimeout.

netadictos
you are right, but I doubt that his AJAX request would take longer than 5 minutes to complete :P
Gabi Purcaru