87027

Multiple ajax calls and too many concurrent processes

Question:

I have 3 ajax calls on a set interval so they update users notification at regular intervals behind the scenes. I had to disable this recently as it caused too many concurrent processes and killed the site on our shared hosting.

I like the functionality of the below but I am wondering if I can do this in a more economic way like nest them in the call backs. So one finishes before the next starts.

The question is, what is the best way to do multiple ajax calls at regular intervals?

setInterval(function() { // check who is online $(".whosOnline").load("ajax/whosOnline.php", function(data) {}); // check for new messages $.ajax({ url: 'ajax/message-count.php', type: "POST", success: function(data) { $(".MessageCountContainer").html(data); } }); // check for new notifications $.ajax({ url: 'ajax/notification-count.php', type: "POST", success: function(data) { $(".NotificationCountContainer").html(data); } }); }, 15000);

Answer1:

You can call the next ajax request in the callback of the previous one, like this :

setInterval(function() { // check who is online $(".whosOnline").load("ajax/whosOnline.php", function(data) { // check for new messages $.ajax({ url: 'ajax/message-count.php', type: "POST", success: function(data) { $(".MessageCountContainer").html(data); // check for new notifications $.ajax({ url: 'ajax/notification-count.php', type: "POST", success: function(data) { $(".NotificationCountContainer").html(data); } }); } }); }); }, 15000);

But, I think it is easier and more efficient, to join together the three call on the server side and put the result in a json format. Then client side you can use it like this :

setInterval(function() { $.ajax({ url: 'ajax/get-all-notification.php', type: "POST", dataType : "json", success: function(data) { $(".MessageCountContainer").html(data.MessageCountContainer); $(".NotificationCountContainer").html(data.NotificationCountContainer); } }); }, 15000);

Answer2:

Try

var timeout = null; var _notifications = true; // check who is online var a = function() { return $(".whosOnline").load(url, function(data) {}).promise() }; // check for new messages var b = function() { return $.ajax({ url: url, type: "POST", success: function(data) { $(".MessageCountContainer").html(data); } }) }; // check for new notifications var c = function() { return $.ajax({ url: url, type: "POST", success: function(data) { $(".NotificationCountContainer").html(data); } }) }; var count = 0; var notifications = function notifications() { return $.when(a()).then(b).then(c).then(function() { ++count; console.log(count); // stop `notifications` // if (count === 5) { // clearTimeout(timeout); // _notifications = false; // }; timeout = setTimeout(function() { if (_notifications !== false) { notifications() }; }, 15000) }) }; notifications();

<pre class="snippet-code-js lang-js prettyprint-override">var timeout = 0; var _notifications = true; var url = "https://gist.githubusercontent.com/guest271314/6a76aa9d2921350c9d53/raw/49fbc054731540fa68b565e398d3574fde7366e9/abc.txt"; // check who is online var a = function() { return $(".whosOnline").load(url, function(data) {}).promise() }; // check for new messages var b = function() { return $.ajax({ url: url, type: "GET", success: function(data) { $(".MessageCountContainer").html(data); } }) }; // check for new notifications var c = function() { return $.ajax({ url: url, type: "GET", success: function(data) { $(".NotificationCountContainer").html(data); } }) }; var count = 0; var notifications = function notifications() { return $.when(a()).then(b).then(c).then(function() { ++count; console.log(count); if (count === 5) { clearTimeout(timeout); _notifications = false; }; timeout = setTimeout(function() { if (_notifications !== false) { notifications() }; }, 15000) }) }; notifications(); <pre class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="whosOnline"></div> <div class="MessageCountContainer"></div> <div class="NotificationCountContainer"></div>

Recommend

  • Get or convert Week of year to ISO week
  • How to distribute Java-based software?
  • If I am creating a simple client server application in IntelliJ, how should this work?
  • Cuda Clang and OS X Mavericks
  • Exception gevent.hub.LoopExit: LoopExit('This operation would block forever',)
  • Possible to “watch” both HAML and SASS at the same time?
  • Django model inheritance, filtering models
  • Jquery popup on mouse over of calendar control
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Check for zero lines output from command over SSH
  • Loading .coffee files via a view in Rails
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Create DicomImage from scratch using Dcmtk
  • Is there a way to do normal logging with EureakLog?
  • PHP buffered output depending on server setting?
  • How to clear text inside text field when radio button is select
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Python CGI os.system causing malformed header
  • Ajax Loaded meta Tags
  • Recording logins for password protected directories
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Xamarin Forms - UWP Fonts
  • Android screen density dpi vs ppi
  • Splitting given String into two variables - php
  • Bug in WPF DataGrid
  • Check if a string to interpolate provides expected placeholders
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Jquery - Jquery Wysiwyg return html as a string
  • Confusion with PayPal's monthly billing cycle
  • Arrays break string types in Julia
  • A cron job substitute?
  • json Serialization in asp
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?