Polling AJAX is tricky. If the next interval triggers before the last AJAX request is complete, your app is doomed!! This creates a domino effect where your queue for requests fill up faster then it can complete. Your queue will NEVER finish… similar to an infinite loop.
In light of this, you should never put AJAX into a setInterval function. A clean, more robust option is this:
Notice the “complete” event of the AJAX triggers the self-executing function. Instead of using setInterval, it is using setTimeout that waits for 5 seconds then calls the AJAX. The process starts again when the “complete” triggers, waits 5 seconds, calls the AJAX again, then complete triggers once more, and so forth. Your queue of requests will always be one. If you study this, you will realize how clean this is!
- Simple AJAX Polling Plugin for jQuery
- JQuery PeriodicalUpdater (AJAX long polling/server polling)
- jQuery AJAX Documentation
- HTML5 Web Sockets (the future!)
Latest posts by Basem Emara (see all)
- New Kendo UI Media Player Widget with MVVM - August 21, 2014
- How to Customize Sitefinity Search Templates - August 7, 2014
- Kendo UI Mobile with MVVM and RequireJS - August 4, 2014
- HTML Imports is the New Dependency Management for the Web - July 16, 2014
- Falafel Software Furthers Commitment to Sitefinity Developers with New Software Development Kit - July 15, 2014