0

我目前正在使用 JQuery 读取包含照片幻灯片数据的外部 .json 文件

照片幻灯片.json

 [ 
        {
            "title" : "my tile", 
            "image" : "xx.jpg", 
            "url" : "www.example.com",
            "firstline" : "woow", 
            "secondline" : "the weather is fine"
        },

       .....
       ..... 
    ]

    <script type="text/javascript" >
    var photos; 
    $.getJSON(
           "lang/en/photo-slideshow.json", 
            function(result) {
                    photos = result;
            }
       );
    <script>

该脚本在加载时间较长的页面上运行良好!在页面加载速度非常快(例如 1 秒)时,json 文件不会t seem to be read completely and the slideshow doesn启动。

4

2 回答 2

0

我解决了!我把这一行放到我的代码中: $.ajaxSetup({async:false}); 谢谢

于 2013-10-24T20:51:44.793 回答
0

您是否在触发 json 回调后初始化幻灯片?

当您对 json 对象执行异步调用时,您的幻灯片初始化应该在 getJson 触发的回调函数中,以确保您在需要时确实拥有手头的数据。

你应该这样做:

$.getJSON('path/to/my.json',function(result){
    mySlideShowComponent.init(result);
})
于 2013-10-24T17:24:26.100 回答