多个ajax调用和太多并发进程 [英] Multiple ajax calls and too many concurrent processes

查看:163
本文介绍了多个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调用和太多并发进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆