简体   繁体   English

加载第一页时出现延迟的下拉菜单

[英]drop down menu populating with delay on first page load

I created toggled dropdown menu using bootstrap. 我使用引导程序创建了切换下拉菜单。 The drop down menu items are populated using an ajax call. 使用ajax调用填充下拉菜单项。 The ajax request makes a call to a PHP script which fetches values from the database and populates the dropdown menu items. ajax请求调用一个PHP脚本,该脚本从数据库中获取值并填充下拉菜单项。 I used an unordered list to display items in the drop down menu. 我使用无序列表在下拉菜单中显示项目。 When I click on the button responsible for ajax call, the items are getting populated but with a delay. 当我单击负责ajax调用的按钮时,这些项目正在填充,但有延迟。 When I click on the button again, no delay is being observed. 当我再次单击该按钮时,没有观察到延迟。

JQuery: JQuery的:

$(document).on('click',"#itemsButton",function (e) {
                    e.preventDefault();
                    var osn = $("#osn").val();
                    //$("#items-dropdown").empty();
                    var dataString = 'searchString=' + osn;
                    if ($.trim(osn).length > 0) {
                     $.ajax({//create an ajax request to load_page.php
                            type: "POST",
                            url: "retrieveItemsOrdered.php",
                            data: dataString,
                            cache: false,
                            dataType: "html", //expect html to be returned
                            success: function (html) {
                                $("#items-dropdown").html(html); 
                            }
                        });
                    }

                });

HTML: HTML:

<div class='itemsmenu btn-group'>";
                    <button type='button' class='btn dropdown-toggle' data-toggle='dropdown' id='itemsButton'>
                    <span class=>Click here to view items&nbsp</span>
                    <span class='pull-right'><i class='fa fa-caret-down'></i></span>
                    </button>
                    <ul class='dropdown-menu ' role='menu' id='items-dropdown'>
                    </ul>
</div>

PHP: PHP:

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
       echo "<li class='ordeinfo-style'>";
       echo "<b>".$row['sku']."</b>";
       echo "</li>";
    }

CSS: CSS:

.dropdown-menu {
        border-radius: 0;
        -webkit-box-shadow: none;
        box-shadow: none
        }       
        .itemsmenu .btn{
    text-align: center;
        }
        ul {
                min-width: 200px;
        }
        .items-dropdown{
            text-align: center;
        }
        ul b{
                font-weight: normal;
                display: inline block;
                font-size: 16px;
                font-weight: bolder;
                color: #000;
        }
        .quantity{
            font-size: 16px;
            margin-right: 20px;
            color: #000;
        }

Please let me understand why I am experiencing delay when button is clicked for the first time to display the drop down menu items 请让我理解为什么第一次单击按钮以显示下拉菜单项时会遇到延迟

update, please see these questions PHP with MySQL is Slow (SOLVED) and Why is the response on localhost so slow? 更新,请查看以下问题: MySQL的PHP​​运行缓慢(已解决)为什么本地主机上的响应如此缓慢?

the below is no longer applicable 以下不再适用

The first ajax call to retrieveItemsOrdered.php will have some delay, depending on the query itself, number of results and where the DB server is located. 首次调用ajax调用retrieveItemsOrdered.php将有一些延迟,具体取决于查询本身,结果数和数据库服务器所在的位置。

The second call, ex. 第二个电话,例如。 second time you click the button, will produce faster results because the db query result will be cached by both the driver (mysqli and the mysql server). 第二次单击该按钮将产生更快的结果,因为db查询结果将同时被驱动程序(mysqli和mysql服务器)缓存。

You could inspect/explain the DB query itself and optimize it to reduce the delay. 您可以检查/解释数据库查询本身,并对其进行优化以减少延迟。

This is natural because you are using AJAX call to get the records from the DB and has nothing to do with any of styles or HTML . 这是很自然的,因为您正在使用AJAX调用从数据库获取记录,并且与任何stylesHTML无关。

Now that call is making a journey all the way down to your DB and give you response back so you are experiencing the delay. 现在,该呼叫一直在您的数据库中进行,并给您回复,因此您遇到了延迟。 This was when you click the button first time. 这是您第一次单击按钮时的情况。 When you click on the button again for a round trip as you see, the response cached by browsers and your previous records were still there in list so delay was almost unnoticeable. 当您再次单击该按钮进行往返时,浏览器缓存的响应和以前的记录仍在列表中,因此延迟几乎不明显。

The delay could be due to many reasons. 延迟可能是由于许多原因。 Either your DB is at some remote location or your DB query took some extra time to evaluate. 您的数据库位于某个远程位置,或者您的数据库查询花费了一些额外的时间进行评估。 It would be even worst if you are dealing with large amount of data. 如果要处理大量数据,那将甚至更糟。

Remember, making an AJAX call does not mean the response would be faster. 请记住,进行AJAX调用并不意味着响应会更快。 Under the covers it is how that response would get handled by the script when it arrives. 隐藏的内容是脚本到达时如何处理该响应。 It appears fast because it is never a full page refresh. 由于从未刷新整页,因此显示速度很快。 You can get help from inbuilt browser developer tools. 您可以从内置的浏览器开发人员工具获得帮助。

Recommendations: 建议:

  • Try optimizing your DB query. 尝试优化您的数据库查询。 Take some help from nice tutorials out there. 从不错的教程中获得一些帮助。 If you still can't figure out the solution then put a nice summary of your problem with your code and put on StackOverflow with relevant details. 如果您仍然找不到解决方案,则对您的代码问题做一个很好的总结,并在StackOverflow上加上相关的详细信息。
  • For a better user experience put a loading spinner or wait message as a blanket. 为了获得更好的用户体验,请将正在加载中的微调器或等待消息用作毯子。
  • This goes to much of a refactoring now. 现在进行了很多重构。 Save the number of calls you made to DB. 保存您对数据库进行的呼叫次数。 Returning JSON would be better rather than HTML . 返回JSONHTML更好。 Your HTML should just display returned data. 您的HTML应该只显示返回的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM