多个ajax调用和太多并发进程

编程入门 行业动态 更新时间:2024-10-26 08:25:15
本文介绍了多个ajax调用和太多并发进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在设定的时间间隔内进行了3次Ajax调用,因此它们会在后台定期更新用户通知.我最近不得不禁用此功能,因为它导致太多的并发进程并杀死了我们共享主机上的站点.

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.

问题是,定期进行多次Ajax调用的最佳方法是什么?

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);

推荐答案

您可以在上一个请求的回调中调用下一个ajax请求,如下所示:

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);

但是,我认为将服务器端的这三个调用结合在一起并以json格式放入结果会更轻松,更高效. 然后客户端可以像这样使用它:

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);

更多推荐

多个ajax调用和太多并发进程

本文发布于:2023-10-28 00:45:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:太多   多个   进程   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!