I'm not sure what you are asking here, but here's some thoughts:
1) Your code seems to contradict what you've written in the title. Using setTimeout
(or setInterval
) for continuous polling is something different then long polling. Actually it is a (normal) polling. The difference is that with long polling client starts an AJAX request and he waits. The server decides when to respond. And it should respond only when new data is available. And immediatly after the respond client starts a new polling request.
Side note: creating (relatively) efficient long polling server is not an easy task, be aware of that.
2) How you handle client side (i.e. where you put long polling logic) doesn't really matter as long as you know what's going on inside your code. Of course keep in mind that maybe in a future you would like to make some changes to the code, so keeping it separate would probably be a best choice. Here's the architecture that I would choose:
- Independent script which creates global
EventManager
object (this script should load as the first one). Such object will have the following methods: .bind
and .trigger
and it will, erm... manage events. :) Here's for example how the implementation may look like:
Implementing events in my own object
- Independent script which handles long polling. Whenever the data is received from the server (i.e. the AJAX long polling request finally ends) it calls
EventManager.trigger('long_polling_data', res);
. Then you need to bind to this event inside your backbone view or wherever you like.
Side note: additional bonus with this architecture is that if you decide to switch to WebSockets or any other technique (for example: JSONP polling) then you will only have to implement the logic for the other technique. The main code will only use long_polling_data
event, so no additional changes will be required (you may want to change the name of the event :] ).
3) Although you say that you don't want to use WebSockets I have to comment on this. :) You know that world is constantly evolving. You should forget about long polling techniquesl. Using WebSockets and XMLSocket (a.k.a. FlashSocket) as a fallback is much more efficient and it is a lot simplier to implement the server side.
I hope I helped a bit, sorry for any language mistakes and good luck with your project!