1

I have a very short question. What happen if I use setInterval method to call an ajax function like checking message in inbox every 3 second (or if I can do in every 1 second would be nice!)

Well this is what I use:

setInterval(function() {
chk_inbx (var1, var2);

}, 8000);

I call this function every 8 second right now. I see no problem. (or maybe I don´t have so many users right now)

But what happen if I change it from 8000 to 3000 or even 1000 ?

UPDATE I have test the polling (LONG POLLING (in php) starts hanging after 5 requests to database )

I wonder what ´s diferences between setTimeout and setTimeout with polling. What I understand is in php file they put the sleep(); function and use timestamp to determin if the data is old or not.

But mine, I use N nd Y to determine if msg is read or not. if N then show the rowCount.

So what is the different ? can anyone help me ?

Community
  • 1
  • 1
Poramat Fin
  • 121
  • 1
  • 10
  • 6
    Then you'd be making 266% more calls. – j08691 Aug 28 '13 at 16:25
  • j08691 Oh, sound horrible now :) But what do you think ? – Poramat Fin Aug 28 '13 at 16:27
  • Use push model ..Instead..Reverse Ajax – Deepak Ingole Aug 28 '13 at 16:28
  • I would recommend looking at another platform if you want real time communication. Look into something called long polling, as this might be a better option. I know that Node.js can do it, and I think there are some python libraries that can handle long polling as well. – Last Rose Studios Aug 28 '13 at 16:28
  • I see many people talked about that now , I will look around there :) Thanks :) – Poramat Fin Aug 28 '13 at 16:40
  • @LastRoseStudios Long polling can be done in any language that has some way to idle the process (`sleep` / `idle timers`), basically any server-side language, including, but obviously not limited to, `Python, PHP, Javascript, C#, C++ and C`. – Paul Aug 28 '13 at 16:40

2 Answers2

3

It could cause issues if the request takes longer to complete than your polling time. The duration of the request will depend on both your user's network speed and geographical location. Browsers will limit the number of concurrent requests, but they will keep queueing the new requests and the queue will grow to an unreasonable size, possibly slowing down or even crashing some browsers.

There are better ways to achieve what you want to do. The most common and cross-browser (websockets could also solve this problem in modern browsers) way is to use long polling.

With long polling you send a request as soon as you receive a response, so you are always polling continuously. The web server handles the process idling and repeated checks for new messages without generating a response until either a timeout (you can pick something like 30 seconds) or there is new data to inform the browser about.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • What if an AJAX request takes more than 3 seconds? It's not a common occurrence but it _will_ happen. Then you end up stacking up AJAX requests. It'd be a good idea to mention starting the next request from the callback of the first, in addition to the (superior) long polling technique – Bojangles Aug 28 '13 at 16:28
  • I really wonder what´s the method Facebook use. I have observed one time. I see it different. Sometime 1 minute sometime shorter. ANd if i can do like every one second, I could make an ajax chat by myself easy. – Poramat Fin Aug 28 '13 at 16:29
  • @PoramatFin Did you check the link on long polling? It will give you instant responses, as you are essentially polling continuously. – Paul Aug 28 '13 at 16:30
  • Now I´m ready -->http://stackoverflow.com/questions/333664/simple-long-polling-example-code – Poramat Fin Aug 28 '13 at 16:32
  • @PoramatFin Yeah, that's a good example. I also added more detail about how long polling works into my answer :) – Paul Aug 28 '13 at 16:37
  • Thanks I will check it more. – Poramat Fin Aug 28 '13 at 16:39
3

You should avoid using setInterval() in this way.

The problem with setInterval() is that it keeps firing the event every 3 seconds (or whatever), even if the previous event hasn't finished yet.

If your response time is slower than the interval time, this can cause major problems with requests piling up on top of each other and maybe not even responding in the same order that they were requested.

This will become more and more noticeable the smaller the interval you set, but it can be a problem even for big intervals if you have any kind of blocking event in your JS code - an alert() box would be the classic example - as you can end up with large numbers of interval events piling up waiting for the alert to be cleared, and then all firing at once.

A better solution is to use a self-firing setTimeout().

This would use setTimeout() to start the event sequence, which then calls another identical setTimeout() call internally when its event is completed. This ensures you will never have multiple events piling up like you can with setInterval().

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • Spudley thank you for the answer ! Before i make this project. I used to think that I will use seTimeout method. But after I look around from google, People said it almost the same / or it the same. So that´s why I end up with setInterval :S – Poramat Fin Aug 28 '13 at 16:38
  • I will check what´s the best between setTimeout and long polling – Poramat Fin Aug 28 '13 at 16:41
  • @PoramatFin `setTimeout` is the easier to implement, but long polling is a better option. There are pros and cons to both. If you want the inbox to function like email and updating once a minute or so is okay, then `setTimeout` is probably the best and simplest way to go. If you want it to be like instant messaging than long polling is the way to go, since you can reduce the number of HTTP requests and have immediate updates. – Paul Aug 28 '13 at 18:44
  • Paulpro I have tried to look around many tutorial out there. It´s quite new for me. And above all I did not use jQuery, I use my own ajax script in my project (Why ? because I like to do all by my hand and avoid many attaching documents in my page) So sound very interesting. Because my goal is to create someting as fast as I can like instant message. Right now I found that setTimeout work good for me. But in the future I don´t know. If you don´t mind to give me example to use the polling with none Jquery user to call function like I did above. Would be nice. – Poramat Fin Aug 29 '13 at 00:25