ExtJS setTimeout调用函数但是在destroy视图之后我得到了一些错误

时间:2014-07-24 16:38:34

标签: extjs extjs4 extjs4.1 extjs4.2

我有一个功能,在3000秒后调用其他功能来检查网格的新数据。

onViewDestroy: function(view) {
        console.log('view destroy');
        var me = this;
        if(me.timer) clearTimeout(me.timer);
    },
    afterRender: function(){

     // start timer to load log grid in 3 secs
         me.logTimer = setTimeout(Ext.bind(me.checkProcessLog, me), 3000);

    },    
    checkProcessLog: function() {
                console.log('check process log');

                var me = this,
                   requestGrid = me.getRequestGrid(),
                   logGrid = me.getProcessLog(),
                   logStore = logGrid.getStore();

                var selectedRecord = requestGrid.getSelectionModel().getSelection()[0];

                // if the grid is expanded and the status of the process is pending or in progress
                if (!logGrid.collapsed && (selectedRecord.get('Status') == 0 || selectedRecord.get('Status') == 1)) {

                    logStore.load({
                        callback: function(records, op, success) {

                            if (success) {

                                // start timer to load again log grid in 3 secs
                                me.logTimer = setTimeout(Ext.bind(me.checkProcessLog, me), 3000);

                            }

                        }
                    });

                }

            },

我遇到的问题是,如果我在调用函数时关闭了我的视图(destroy),那么我的所有变量都是:

  requestGrid = me.getRequestGrid(),
    logGrid = me.getProcessLog(),
    logStore = logGrid.getStore();

我收到错误:

  

Cannot read property 'getStore' of undefined

这是有道理的,因为视图已被破坏。有什么办法可以避免这种情况吗?

1 个答案:

答案 0 :(得分:1)

一个简单的检查就可以了:

requestGrid = me.getRequestGrid(),
logGrid = me.getProcessLog();
if(!logGrid) return false
logStore = logGrid.getStore();

这也将停止调用函数。