Search code examples
extjsextjs4extjs4.1extjs4.2extjs-mvc

ExtJS setTimeout loop function not working (losing scope) Ext.bind


I have a ExtJS 4.2.1 Controller where I fires a function after 1 second using setTimeout:

onPrint: function () {
        var me = this;

        // start some stuff here...

        // end some stuff here...

        // wait 1 second and call checkDownloadComplete function
        setTimeout(me.checkDownloadComplete, 1000);

    },
    checkDownloadComplete: function () {
        var me = this;

        // validate stuff here...
        if (something == "true") {

            return;
        }

        // first approach but didn't work (maybe because the scope)
        setTimeout(me.checkDownloadComplete, 1000);

        // sencond approach but didn't work
        Ext.bind(function () {
            setTimeout(checkDownloadComplete, 1000)
        }, me)


    },

My first try was to use:

setTimeout(me.checkDownloadComplete,1000); But this didn't work

My second try was to comment the last line, and use Ext.bind:

Ext.bind(funciton(){setTimeout(checkDownloadComplete,1000)},me);

Any clue what I'm doing wrong? Because none of those work to call again a function it self.

Thanks

UPDATE:

I also try this but without success:

setTimeout(Ext.bind(checkDownloadComplete,me), 1000);

Solution

  • The solution was to change both setTimeout calls to:

    setTimeout(Ext.bind(me.checkDownloadComplete, me), 200);
    

    The problem was that on the first call of the setTimeout, the checkDownloadComplete scope was changed to a setTimeout scope instead of controller scope, so changing both fixed the problem-