gpt4 book ai didi

javascript - Ext.each 递减循环计数不正确

转载 作者:行者123 更新时间:2023-11-30 15:00:37 25 4
gpt4 key购买 nike

在两个网格同时加载数据的组件(例如选项卡)中,我使用以下函数创建一个全局掩码,该掩码仅在商店全部加载后才会被删除。它通常运作良好。

    testLoadAllStores: function(allStores, component){

var indexStores = 0;

setTimeout(function () {

Ext.each(allStores, function(storeId) {
var store = Ext.getStore(storeId);
if(store){
if(store.isLoading()){
indexStores++
console.log(indexStores);

store.on('load', function() {
indexStores--;
console.log(indexStores);
if (indexStores == 0){
setTimeout(function() {
if(component.isMasked()){
component.unmask();
}
}, 500);
}
});

}
else if(!store.isLoading() && indexStores == 0){
setTimeout(function() {
if(component.isMasked()){
component.unmask();
}
}, 500);
}

}
});
}, 500);
}

在 Controller 中调用函数如下

    var allStores = ['storeOne', 'storeTwo'];
var component = Ext.getBody();
component.mask();
App.util.Util.testLoadAllStores(allStores, component);

但是在以下情况下我遇到了问题:每次选择一行网格时都会显示两个图表。在这种情况下,函数 testLoadAllStores 被调用,并且仅当加载图表存储时才会触发取消屏蔽。

问题是每次我选择一行(selectChange 事件)时,indexStores-- 都会给出以下值(它有效但倒计时 不正确)。

//first selection
1
2
1
0

//second selection
1
2
-1
1
-2
0

// third selection
1
2
-3
-1
1
-4
-2
0

最佳答案

您可以保留老听众,并在顶部添加新听众。这意味着每次加载商店时,旧的听众都会从零倒数到零以下。

为了防止您的商店因监听器而变得杂乱无章,可能会随着时间的推移降低应用程序的速度,您应该将监听器标记为单个,这将在监听器首次触发后将其移除:

store.on('load', function() {
...
}, this, {
single: true
});

此处描述:http://docs.sencha.com/extjs/6.2.1/classic/Ext.Evented.html#method-on--options

关于javascript - Ext.each 递减循环计数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46594363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com