我正在尝试调试一些 javascript 代码。我要调试的变量是对象的 JS 对象,异步加载(ajax 回调)。我在回调之外调试它。
我很确定这个问题是一个竞争条件问题,但我想确定(这不是我的问题)。
我在调试时发现的问题是,它console.log
给了我一个与代码正在做什么没有意义的信息。因此,要么我在代码中遗漏了一些东西,要么我在控制台中看到的结果不是我运行时变量状态的快照console.log
。
如果结果是后者,我怎样才能获得异步加载的 JS 对象状态的快照?
这是代码的简化示例:
//This call takes a while to invoke the callback
$.get("url",function(data){
//Process data
globalVariable = data; //JSON (Object of objects)
}
//Couple lines afterwards
/* This console.log shows (in Firebug's console) "Object { }" and when I click it,
I can see the object with all its fields filled (oher objects)
*/
console.log(globalVariable);
for(var e in globalVariable){
//Does not enter here, meaning that the object is empty
}