JavaScript:变量值在函数之间丢失
-
11-09-2019 - |
题
我有以下代码
function updateSliderContent(json) { //<- json defined here is correct
var screen_order = json.screen_order.split('_');
jQuery.each(screen_order, function(i, item) {
var screen_id = item;
//at this point it is not, thus the function does not execute whatever is in the if blocks
if (json[screen_id].action == 'add') {
//doSomething
} else if (json[screen_id].action == 'remove') {
//doSomthingElse
};
}
}
我的问题是,不知何故,json 的值(它是来自 AJAX 调用的对象)在 jquery 的每个函数中丢失了。我还没找到原因,也不知道如何解决。谷歌没有给我我正在寻找的答案。
编辑1
这是实际的调用。
function updateSlider() {
var screenOrder = '';
jQuery('div#slider td').each(function(i, item) {
screenOrder += this.abbr + '_';
})
var ajaxData = {
sid: sid,
story: story,
date: theDate,
screenOrder: screenOrder,
mode: 'ajax_update_slider'
};
jQuery.ajax({
data: ajaxData,
dataType: 'json',
success: function (json) {
updateSliderContent(json);
}
});
theDate = Math.round(new Date().getTime()/1000.0); //UNIX Timestamp
sliderTimer = setTimeout('updateSlider();',15000);
};
解决方案
我尝试在 JS Bin 上重现您的问题,但失败了:
http://jsbin.com/ereha (可通过编辑 http://jsbin.com/ereha/edit)
到目前为止,您向我们展示的代码看起来完全没有问题,因此问题一定是由代码或系统的其他部分引起的。如果我们不知道问题出在哪里,我们都是在摸黑。
请尝试重现该问题 http://jsbin.com 我们可以从那里为您提供帮助。
完整的源代码
索引.html
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>http://stackoverflow.com/questions/1831384/javascript-variable-value-gets-lost-between-functions</title>
<script type="text/javascript" src="http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$.ajaxSetup({url: 'test.json'});
function updateSliderContent(json) { //<- json defined here is correct
var screen_order = json.screen_order.split('_');
jQuery.each(screen_order, function(i, item) {
var screen_id = item;
//at this point it is not, thus the function does not execute whatever is in the if blocks
if (json[screen_id].action == 'add') {
console.log(screen_id, 'action add');
} else if (json[screen_id].action == 'remove') {
console.log(screen_id, 'action remove');
};
});
}
function updateSlider() {
var ajaxData = {};
jQuery.ajax({
data: ajaxData,
dataType: 'json',
success: function (json) {
updateSliderContent(json);
}
});
// theDate = Math.round(new Date().getTime()/1000.0); //UNIX Timestamp
sliderTimer = setTimeout('updateSlider();',15000);
};
$(updateSlider);
</script>
</head>
<body>
</body>
</html>
测试.json
{
'screen_order': 'foo_bar_baz',
'foo': {
'action': 'add'
},
'bar': {
'action': 'add'
},
'baz': {
'action': 'remove'
}
}
其他提示
有似乎是没有错jQuery.each,因为我无法重现你的问题。
function alertName (json) {
var a = new Array();
a.push(1);
a.push(2);
jQuery.each(a, function(i, item) {
alert(json[0].name);
alert(json[1].name);
});
}
var andre = { name: "André" }
var joana = { name: "Joana" }
var arrayOfJson = new Array();
arrayOfJson.push(andre);
arrayOfJson.push(joana);
alertName(arrayOfJson);
alertName()
功能的工作原理完全因为它应该。将JSON参数不jQuery.each
函数内丢失
这似乎是具体的实现,你不告诉我们的东西有问题。请尝试像我一样“压缩”自己遇到的问题工作示例,并显示我们,使我们可以尝试在我们自己的:)
您确定json
是一个对象。也许它的JSON字符串?应该比你在使用前eval
它。结果
如果您通过电话$.ajax()
得到它不要忘了添加dataType:'json'
作为一个选项...
在托盘如果()强>子句中使用的 item.action 下,因为如果JSON数据是对象的数组,的项目强>将包含的每个对象,但这只是我的假设
我想你应该确保功能的 updateSliderContent 从服务器端的JSON响应后调用。
下面的代码不应该工作:
var json = {};
$.getJSON("/url/", {}, function(data){
json = data;
});
//we call our function before the server response
//and the json will be just blank object
updateSliderContent ( json );
所以,请你的代码看看,因为有时我们不喜欢的东西,如果没有意向。
下面的代码应工作和JSON对象不能空白如果服务器确实响应正确的JSON。
$.getJSON("/url/", {}, function(json){
//we call only after the response from server
updateSliderContent ( json );
});