我基本上是使用Ext.Function.defer向数组中添加元素,并且这些元素正被添加到数组中,但是当我调出数组时,它会输出空数组。这是我的代码:
var arr = [];
Ext.Function.defer(function () {
Ext.Array.forEach(me.query('grid'), function (grid) {
arr.push(grid.collapseTool.getId()); // [1 ,4 , 6 , 8 ,10]
});
}, 1000);
console.log(arr); // outputs empty array []
谁能告诉我我所缺少的吗?先感谢您!
最佳答案
这是因为defer将异步运行,因此您正在访问arr
的状态下未填充您想要的数据
编辑:您可以测试此代码以了解延迟
Ext.Function.defer(function() {
console.log("You will see this message later although it is written first");
}, 1000);
console.log("You will see this message first although it is written after");
Edit2:示例代码
Ext.Function.defer(function() {
var arr = [];
Ext.Array.forEach(panel.query('grid'), function(grid) {
arr.push(grid.collapseTool.getId());
});
onDone(arr);
}, 1000);
function onDone(arr) {
console.log(arr);
}
关于javascript - 为什么在使用Ext.Function.defer(setTimeout)时无法访问数组的值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47123734/