Berpcor2 for loops don’t.

 
Dececco: Szychowski: Could you explain, please.

Szychowski: Lol i just said i’m going to – one sec.

Szychowski: What do you think it is?

Fahner: Szychowski: Each time I fill my array by new values.

Szychowski: That’s not the issue.

Poellot: Each new value is a result of for loop.

Szychowski: That’s not why your stuff is broken.

Szychowski: First of all, you never declare i, so it’s global. also, .load is async. so what happens is you queue up var img = $’img src=”‘ + mgallery.wardrobe + ‘/’ + i + ‘.jpg’ + ‘”/’.loadfunction { 7 times. when that callback fires, i has been set to 7.

Szychowski: And that was its last value.

Katz: Szychowski: Do not tell me the correct variant yet

Lagrasse: I’ll try to write it by myself.

Rump: For i = 1; i = 7; i++ – i = 1 It is declaration, isn’t it?

Szychowski: You never do var i = whatever

Szychowski: But that alone won’t fix your problem.

Fergason: I need to remove load.

Szychowski: That won’t fix it either.

Szychowski: You can keep using .load

Huckabone: I need to move load outside of for loop.

Szychowski: You’re really stretching.

Szychowski: Do you understand what “scope” means in js?

Dahmer: Szychowski: I think I do

Szychowski: Do you see how that would apply here?

Petr: Szychowski: Besides, this is the thing I do not understand fully yet.

Szychowski: So do you understand my explanation above?

Dunworth: I think I understand what do you mean, but I do not have solution yet.

Vehrs: This is number of images in folder.

Denise: I know, this is not the best approach.

Szychowski: If you had an array to work with here instead, this would be a really easy thing to fix.

Szychowski: Then you could use .forEach instead and you wouldn’t have this problem.

Cittadini: Szychowski: I think it is not problem at all to use array here.

Makepeace: But what about my current variant?.

Szychowski: So first of all change for i = 1; i = 7; i++ { to for var i = 1; i = 7; i++ {

Szychowski: And then try, in the .load callback, something like var currentIndex = i; then use currentIndex instead of i within that function

Szychowski: Alternatively, you could create a function that handles the whole var img = $’img src=”‘+mgallery.wardrobe+’/’+i+’.jpg’+'”/’.loadfunction{ stuff and p*** the index to that instead.

Kaid: Step by step. ‘var’ is declaration of variable in current scope instead of global?

Szychowski: Correct. you should ALWAYS declare with var.

Szychowski: You should never use global vars.

Janowiec: Szychowski: The reasons not to do it are the same as in php?

Szychowski: I don’t know PHP so I can’t really answer that.

Snide: I followed your first directive.

Scelsi: Ok, I understand, too many questions. Nevertheless, thank you very much for help. I’ll try all variants.

Sinagra: Berpcor2: I’m having trouble grasping your problem with the join/quit spam. Will you re-state it, please?

Spellman: Http://jsbin.com/firijubuse/edit?html,js,output

Szychowski: Bazan basically, they were using the number from a for loop to do something inside of a .load

Szychowski: But they were getting 7 the final number for everything after the callback fired.

Szychowski: They just need to get the number into the load callback scope.

Sorgatz: Szychowski: http://jsbin.com/pigikamele/edit?html,js,output

Dubill: Function definition is inside the loop. Move it outside and refer to it instead.

Szychowski: Berpcor2 for loops don’t have scope.