Reputation: 2495
I have a question about using setInterval(); in javascript. I am developing a hospital's (PHP/SQL, Ajax)database, now whenever a pateints test is carried out It should display a message on the main screen saying "(Pateints Name)'s Test Results updated". I tried to do it with php script but there was no way to trigger an event directly from php script. Now I am using setInterval() for this purpose which run a function which checks the pateints database every 10 seconds and display the message when new tests are added.
setInterval(checkAndDisplayNewResults(), 10000);
My question is that " Is it the correct way to do it? Wouldn't it overload the server or client PC? If its not the right way then whats the alternative?
Upvotes: 1
Views: 1006
Reputation: 127
I think that you will overload the server more. Especially if there are going to be a lot of connections from different places. Using Comet technology might help:
Upvotes: 1
Reputation: 4912
In complement to jfriend00's answer, I would suggest to look for another solution than using setInterval
to do what you want.
setInterval
is considered harmful for some reason:
setInterval
ignores errorssetInterval
does not care about network latencysetInterval
does not guarantee executionYou can look there to learn more http://zetafleet.com/blog/why-i-consider-setinterval-harmful.
If you have really no other choice than using this technique for checking for server updates, a code like the one below would be more efficient
var timer = null,
delay = 1000;
function checkAndDisplayNewResults() {
// do some work
// when the work is finished, set a timer to call again the function
timer = setTimeout( checkAndDisplayNewResults, delay );
// the term "when the work is finished" means in this case, when your AJAX request is finished
// and not simply at the end of the checkAndDisplayNewResults function
}
// call the function immediately
checkAndDisplayNewResults();
Also, as said by Joseph, be aware of the bandwidth overhead this technique might bring to your application.
In any case, as others have pointed out, there are other better ways to do this: WebSocket, Server-Sent Events, HTTP polling, AJAX Push, long loading iframe etc... You can check out the intro of this article about Web Sockets http://www.html5rocks.com/en/tutorials/websockets/basics/ and read more to learn how to use Web Sockets
Upvotes: 0
Reputation: 392
The server should handle 1 request every 10 seconds wiht no problems, I think this is self-explanatory.
A client will not have any problems with it either. Only make sure to get rid of DOM elements like Joseph said, or else the RAM would slowly fill up.
It all boils down to how many clients you have.Note that a webserver can handle quite a lot of requests, so don't be afraid to make use of it. But you didn't mention anything about how many clients there is.
If there are a lot of clients the only thing that comes to my mind is the database, how heavy is that query? Should the results be cached? Every 5-10 seconds maybe?
If using AJAX is the easiest way for you to acomplish this, then I say go for it :)
Upvotes: 0
Reputation: 5100
If you use setInterval make sure you set the interval only once. Otherwise you may get multiple instances.
You can search for websockets and see if that may be a solution for you
Upvotes: 0
Reputation: 707546
Polling a server every 10 seconds forever is rarely the right way to get updates. It's enormously inefficient for both the client and the server and the network. You should either back the polling interval way back to a much slower time interval or implement something much smarter like a server-push solution.
One such solution that is more efficient is Comet which consists of long running server requests that will return data as soon as the server makes the data available.
HTML5 also specifies a WebSocket interface which allows the client and server to just have a socket open between the two and freely send data either way upon demand.
Upvotes: 5
Reputation: 119847
The problem lies not on the setInterval()
because it just executes the code.
The problem lies in:
HTTP requests
Polling every 10 seconds is fine on a single PC. But if you have multiple PCs polling your server, it will eat up your bandwidth as well as add overhead to the server due to the multiple requests. Imagine a whole hospitals worth of PCs bombing your server every 10 seconds.
The browser DOM
Also, if this is a kiosk application, you should also worry about the number of items you are loading into the DOM. Like say in an airport "flight schedule screen", you just add more and more items to the list. If you don't remove those unused items, it will eat up memory.
Upvotes: 0
Reputation: 12043
Yes this is the right way, you have no other choice, Gmail for example use the same technique to show you new emails.
Upvotes: -2
Reputation: 108500
It depends on what the function does, but most likely not if it’s a 10 secs interval. You should probably write it like this though:
setInterval(checkAndDisplayNewResults, 10000);
Upvotes: 0