同时发送多个ajax,时间响应很慢

时间:2018-07-25 03:01:24

标签: c# angularjs ajax asp.net-web-api

在我当前使用ASP.Net和AngularJs的项目中。现在我有一个关于服务器时间响应的问题。 当加载页面时,我发送2个请求ajax以获取数据。 如果我同时发送函数getTopicInfo()getAnswerList()

$scope.init = function (categoryId, topicId, answerId, commentId, userId) {
    $scope.categoryId = categoryId;
    $scope.topicId = topicId;
    $scope.answerId = answerId;
    $scope.commentId = commentId;
    $scope.userId = userId;
    $scope.currentPage = 1;
    $scope.itemsPerPage = 10;
    $scope.orderBy = getTopicOrderBy();

    getTopicInfo();
    getAnswerList();
}

时间响应很慢。

enter image description here

如果200毫秒后调用函数getAnswerList()

$scope.init = function (categoryId, topicId, answerId, commentId, userId) {
    $scope.categoryId = categoryId;
    $scope.topicId = topicId;
    $scope.answerId = answerId;
    $scope.commentId = commentId;
    $scope.userId = userId;
    $scope.currentPage = 1;
    $scope.itemsPerPage = 10;
    $scope.orderBy = getTopicOrderBy();
    getTopicInfo();
    setTimeout(function () {
        getAnswerList()
    }, 200);
}

时间响应快 enter image description here

我真的不明白。我花了很多时间。

功能代码getTopicInfo()getAnswerList()

function getTopicInfo() {
    var data = {};
    data.CategoryId = $scope.categoryId;
    data.TopicId = $scope.topicId;
    $.ajax({
        type: "POST",
        url: link("/Topic/GetInfo"),
        data: data,
        success: function (data) {
            $scope.topicInfo = data.Data.TopicInfo;
            $scope.load += 1;
            $scope.$apply();
            addListener();
        }
    });
}
function getAnswerList(page, scroll) {
    page = page || 0;
    scroll = scroll != false ? true : false;
    var data = {};
    data.CategoryId = $scope.categoryId;
    data.TopicId = $scope.topicId;
    if (page == 0) {
        data.CurrentPage = $scope.currentPage;
        data.AnswerId = $scope.answerId;
        data.CommentId = $scope.commentId;
    } else {
        data.CurrentPage = page;
        data.AnswerId = 0;
        data.CommentId = 0;
    }
    data.ItemsPerPage = $scope.itemsPerPage;
    data.OrderBy = $scope.orderBy;
    $.ajax({
        type: "POST",
        url: link("/Answer/GetList"),
        data: data,
        success: function (data) {
            $scope.answerList = data.Data.AnswerList;
            $scope.currentPage = data.Data.CurrentPage;
            $scope.totalItems = data.Data.TotalItems;
            $scope.load += 1;
            $scope.$apply();
            if (page == 0 && $scope.answerId != 0) {
                setTimeout(function () {
                    scrollToId("answer" + $scope.answerId);
                    if ($scope.commentId != 0) {
                        var commentId = $scope.commentId;
                        if (!$("#comment" + commentId).isInViewport()) {
                            scrollToId("comment" + commentId);
                        }
                        $("#comment" + commentId).css("background-color", "orange")
                        $("#comment" + commentId).animate({ 'backgroundColor': 'rgba(0, 0, 0, 0)' }, 1000);
                    }
                }, 10);
            }
            if (page != 0 && scroll) {
                scrollToId("answers-header");
            }
            addListener();
        }
    });
}

0 个答案:

没有答案