Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript作用域和新数组()_Javascript_Json - Fatal编程技术网

Javascript作用域和新数组()

Javascript作用域和新数组(),javascript,json,Javascript,Json,我知道我在我的javascript代码中违反了范围规则,并且我非常确定我知道哪里出了问题。我只是不明白我做错了什么或如何纠正它的规则 我正在创建一个具有照片库的应用程序。系统一次显示9张照片。当用户想要查看新照片时,他可以单击屏幕右侧的箭头。此操作向服务器查询更多照片,这些照片随后作为json列表返回。如果用户单击屏幕左侧的箭头,他可以查看以前看到的照片。因此,缓存客户端的所有照片URL是有意义的 这种缓存是在名为photoData的全局变量中完成的(我知道这很糟糕,但我不理解JS,也不知道如何

我知道我在我的javascript代码中违反了范围规则,并且我非常确定我知道哪里出了问题。我只是不明白我做错了什么或如何纠正它的规则

我正在创建一个具有照片库的应用程序。系统一次显示9张照片。当用户想要查看新照片时,他可以单击屏幕右侧的箭头。此操作向服务器查询更多照片,这些照片随后作为json列表返回。如果用户单击屏幕左侧的箭头,他可以查看以前看到的照片。因此,缓存客户端的所有照片URL是有意义的

这种缓存是在名为photoData的全局变量中完成的(我知道这很糟糕,但我不理解JS,也不知道如何让它工作)。photoData是一个二维数组,包含出现特定照片的照片集(基本上是用户在到达该照片之前单击右箭头的次数)以及该照片集中出现的所有照片URL。我遇到了一个问题,照片将在我的GetNewPhotos()函数中正确填充photoData,但在函数超出范围后,照片将离开数组

在我发布代码之前,请允许我说,欢迎任何批评。我两天前开始使用javascript,但并不擅长。幸运的是,jQuery完成了大部分工作

这是我的密码:

var photoData = new Array();
var currentPhotoSet = 1;
var maxPhotoSet = 1;

//I originally tried using a closure to avoid global variables and it wasn't working properly, but this is still lingering here.
(function() 
{
    $(document).ready(function()
    {
        //Irrelevant functions removed for clarity's sake
        GetNewPhotos();
    });
})();

function GetNewPhotos()
{
    $("#right-arrow").click(function(event) 
    {
        currentPhotoSet++
        if (currentPhotoSet <= maxPhotoSet)
        {
          // Load photos from cache
        }
        else
        {
            $.ajax({
                 type: "POST",
                 url: "/match/",
                 data: {"arrow" : "right", "currentPhotoSet" : currentPhotoSet},
                 dataType: "json",
                 success: function(jsonObject) 
                 { 
                    photoData[currentPhotoSet] = new Array();
                    photoData[currentPhotoSet] = jsonObject;
                 }
            });
        }
        SwapOnscreenPhotos(currentPhotoSet)
    });
}

function SwapOnscreenPhotos(currentPhotoSet)
{
    $("#photo-list img").each(function(index) 
    { 
        $(this).attr("src", photoData[currentPhotoSet][index+1]); 
    }); 
}
var photoData=new Array();
var currentPhotoSet=1;
var maxpotset=1;
//我最初尝试使用闭包来避免全局变量,但它没有正常工作,但这种情况仍然存在。
(功能()
{
$(文档).ready(函数()
{
//为了清晰起见,删除了不相关的功能
GetNewPhotos();
});
})();
函数GetNewPhotos()
{
$(“#右箭头”)。单击(函数(事件)
{
当前照片集++

if(currentPhotoSet
ajax
是异步的。`SwapOnscreenPhotos(currentPhotoSet)`不等待来自服务器的数据,只要单击了
#右箭头
,就立即运行。尝试在ajax的
success
事件中添加此选项。非常好的一点!我会尝试。我犯了一个新手错误。“在这个作用域中声明数组是否意味着当我离开这个函数时它超出了作用域?”不。事实上你根本没有使用那个数组。不需要行
photoData[currentPhotoSet]=new array();
,因为下一行
photoData[currentPhotoSet]=jsonObject;
不会添加到新数组中,它会更改
photoData[currentPhotoSet]
,因此它现在引用
jsonObject
,而不是新数组。(无关提示:使用
[]
声明新空数组,而不是
新数组()
,例如
var someArray=[];