1. #1
    Sencha User
    Join Date
    May 2013
    Posts
    25
    Vote Rating
    1
    MYlover is on a distinguished road

      0  

    Default loop for fn function always return last value

    loop for fn function always return last value


    some problem thing here
    Code:
    for (i =1; i<=4; i++) {
                        picturesListeners.push({
                            element: 'element',
                            delegate: 'div.img'+i,
                            event: 'tap',
                            fn: function() {
                                console.log(i);
                            }
                        });
                    }
    it always return 4 no matter which div img clicked. Anyone know why?

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,801
    Vote Rating
    834
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Where does the a variable get defined?
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    May 2013
    Posts
    25
    Vote Rating
    1
    MYlover is on a distinguished road

      0  

    Default


    Opss, sorry it should be 'i' instead of 'a' here. But it's not the root cause..

  4. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,801
    Vote Rating
    834
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    This is how JavaScript works, the variable's value changes with each iteration so when the for loops ends, the value of i is 4
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  5. #5
    Sencha User
    Join Date
    May 2013
    Posts
    25
    Vote Rating
    1
    MYlover is on a distinguished road

      0  

    Default


    hmm but the div class are all correct which is img1, img2, img3 and img4 from this loop. Just the fn: function will always take the last value which is 4. any idea so that the output will be 1, 2, 3 and 4? Thanks

  6. #6
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,833
    Vote Rating
    609
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    You can use this pattern to capture the index:

    Code:
    var fns = [], 
        i = 0;
    for (i = 0; i < 4; i++) {
        fns.push((function(captured) {
            return function() {
                console.log(captured);
            }
        })(i));
    }
    fns[0]();
    fns[1]();
    fns[2]();
    fns[3]();
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  7. #7
    Sencha User
    Join Date
    May 2013
    Posts
    25
    Vote Rating
    1
    MYlover is on a distinguished road

      0  

    Default


    hmm i need to tap the div and show the value... sorry i'm new in sencha.. haha~

  8. #8
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,833
    Vote Rating
    609
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Code:
    for ( i = 1; i <= 4; i++) {
        picturesListeners.push({
            element: 'element',
            delegate: 'div.img' + i,
            event: 'tap',
            fn: (function() {
                return function(captured) {
                    console.log(captured);
                };
            })(i)
        });
    }
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  9. #9
    Sencha User
    Join Date
    May 2013
    Posts
    25
    Vote Rating
    1
    MYlover is on a distinguished road

      0  

    Default


    Thanks

  10. #10
    Sencha User
    Join Date
    May 2013
    Posts
    25
    Vote Rating
    1
    MYlover is on a distinguished road

      0  

    Default


    found the issue~ thanks

Thread Participants: 2

Tags for this Thread

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi