使用从作用域外部的Ajax调用的变量

时间:2014-12-06 20:04:56

标签: javascript html ajax instagram-api

我需要帮助从jquery ajax调用返回,我无法将变量传递出范围。 我在一个带有计时器的数组上有一系列图像,最后一个图像是从ajax调用传递的变量。

记录:获取https://domain.com/undefined?6 404(未找到)

感谢。

               $(document).ready(function() {

               // slideshow
               var preload = [];
               var i;
               var current = 0;
               var slideshowInterval;
               var instagramInterval;
               var image;

               // instagram
               var settings = {
                  hashtags: "foo",
                  client_id: "", // instagram API key
                  images_to_fetch: 1, // fetch 1 images every request to instagram
               }

               function instagram() {
                  $.ajax({
                     dataType: "jsonp",
                     url: "https://api.instagram.com/v1/tags/" + settings.hashtags + "/media/recent",
                     data: {
                        access_token: null,
                        client_id: settings.client_id,
                        count: settings.images_to_fetch
                     },
                     success: data
                  });

               }

               function data(response) {
                     photos = response.data;
                     console.log("Number of pictures fetched: " + photos.length);
                     for(var i = 0; i < photos.length; i++) {

                        photo = photos[i];
                        image = photo.images.standard_resolution.url; // get photo url
                     }

                     console.log("new instagram image ======> : " + image);                  

                  } // data response


               var path = [ // relative paths of images
                  'images/cat.jpg',
                  'images/fox.jpg',
                  'images/dog.jpg',
                  image // passed variable from ajax
               ];

               //preload images    
               for(i = 0; i < path.length; i++) {
                  preload[i] = new Image();
                  preload[i].src = path[i] + "?";
               }

               function slideshow() {
                  var slideshow = $("#slideshow");

                  slideshow.fadeTo('fast', 0, function() {
                     slideshow.css({
                        'background': 'url(' + preload[current++ % preload.length].src + new Date().getDate() + ')'
                     });
                  }).fadeTo('fast', 1);
               }


               setInterval(instagram, 15000);

               setInterval(slideshow, 15000);

            });

0 个答案:

没有答案