2

我正在使用 phonegap 开发一个 android 应用程序,它在页面加载时调用一个 api,获取一个 json 对象作为返回参数。

现在我必须通过从收到的对象中提取值来使用 jQuery mobile 构建页面。

所以我在问什么是可以减少加载时间的最佳实践。

感谢您的帮助。

目前我在做什么

<script>
    $(document).ready( function() {
        $.ajax({
            url : "demourl.com",
            type : "GET",
            success : function(data) {
                var obj = $.parseJSON(data);
                $("#results").html(obj.messagedetails[0].spamReason.userApprove);},
            fail : function() {
                $("#notification").text("Try again after some time.");
            }
        });
    });
</script>

从此调用中获取对象并将其设置为

<div id="results"></div>
4

1 回答 1

2
  1. There's nothing you can do about $.ajax / $.getJSON loading time, it will depend on your internet connection.

  2. Never load data during the page transitions, do it ether before page change or after a page change. Best practice is to do it before page change (just show AJAX loader to indicate AJAX content is loading).

  3. When data is loaded use .append( and not .html( to append data.

  4. In case you are using each or for loop to add a dynamic content (laoded with an AJAX) UNDER NO CIRCUMSTANCES enhance page markup (apply jOuery Mobile style to new added content) during each loop, THIS IS A HUGE TIME SINK, do it only after all content has been added.

    You can find more about this in my ARTICLE, or find it HERE.

  5. Do not use document ready, it could trigger before page is loaded. Instead use correct page event.

    Find more about document ready vs page events in my other ARTICLE, here you will also find a way to benchmark jQuery Mobile and some statistic about how much time is needed for some jQM actions.

于 2013-02-27T11:35:58.240 回答