我正在尝试更新我得到的响应中的全局变量。我知道我的范围被搞乱了,我试着找到一个例子,但是找不到与之匹配的例子。我是javascript新手,所以任何语法提示都会有帮助。任何帮助将是伟大的!函数中的变量不会更新全局
hit_count: function(permalink) {
var count = 0,
perm = permalink,
url;
if(perm) {
url = 'http://www.mysite.com?q=scope:' + perm + '&callback=?';
$.getJSON(url, function(result){
count = result.count;
//want this to update global count or at least return this count
if(count > 1) {
count = count - 1;
}
});
}
return count;
}
这不工作? – Cilan
'$ .getJSON'是异步的,所以这不起作用。当你执行'return count'时,'$ .getJSON'可能还没有完成。你能告诉我们你是如何使用这个功能的吗? – putvande
@putvande我字面上只是从另一个函数调用它为count = parseInt(trend_count(params.permalink),5); – atsituab