JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 20th, 2012, 04:34 AM
Grandum Grandum is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 Grandum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 35 sec
Reputation Power: 0
[Solved] Closure inside a loop

Leaving this here for posterity: I didn't actually have to resort to a new function (!!!) all I had to do was what itborg said and change

context.drawImage(imgObj, wehey, goooo);
to
context.drawImage(this, wehey, goooo);

Big thanks, guys truely appreciate the help. I was way to focused on adding another function to the mix, that I forgot about that part.



I'm having a hard time trying to solve this. I suspect that it has to do with closure inside a loop, but I can not for the life of me get it to work...I have been throwing everything I have at it, I've tried to add a new function to it with return function() etc, etc. nothing works. Please help:


Code:
<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
      #myCanvas {
        border: 1px solid #9C9898;
      }
    </style>
  </head>
  <body>
    <canvas id="myDrawingCanvas" width="825" height="700"></canvas>
    <script>
var imgs = new Array();
imgs[0] = 'black0.png';
imgs[1] = 'black1.png';
imgs[2] = 'black2.png';
imgs[3] = 'black3.png';
imgs[4] = 'black4.png';
imgs[5] = 'black5.png';
var wehey = 0;
var cellswide=2;
var cellshigh=2;
var thisWidth = 50;
var thisHeight = 50;
var goooo = 0;
var i = 0;

drawingCanvas = document.getElementById('myDrawingCanvas');
context = drawingCanvas.getContext('2d');

	for(cellwidthcount=0;cellwidthcount<cellswide*cellshigh;cellwidthcount++){
		var imgObj = new Image();
		imgObj.onload = function() {
if(wehey == cellswide * thisWidth){
wehey = 0;
goooo = goooo + thisHeight;
}
context.drawImage(imgObj, wehey, goooo);
wehey = wehey + thisWidth;
}
	i = i + 1;
        imgObj.src = imgs[i];
	}

    </script>
  </body>
</html>

Last edited by Grandum : November 20th, 2012 at 12:24 PM. Reason: solved

Reply With Quote
  #2  
Old November 20th, 2012, 05:24 AM
Grandum Grandum is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 6 Grandum User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 35 sec
Reputation Power: 0
I just realized I forgot to write what my problem was

imgObj.src = imgs[i];

will not change when looping.

I want it to loop out this:
imgObj.src = imgs[1];
imgObj.src = imgs[2];
imgObj.src = imgs[3];
imgObj.src = imgs[4];

But all I get is
imgObj.src = imgs[4];
imgObj.src = imgs[4];
imgObj.src = imgs[4];
imgObj.src = imgs[4];

any help would be greatly appreciated.

Reply With Quote
  #3  
Old November 20th, 2012, 06:38 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,858 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 16 h 34 m 47 sec
Reputation Power: 813
Hi,

yeah, that's a common problem. A loop does not introduce a new scope, so when you define a new variable inside the loop, it's in fact always the same variable that belongs to the scope the loop is in.

As an example:
Code:
var functions = [];
for (var i = 0; i < 2; i++) {
	var x = i;
	functions.push(function () {
		alert(x);
	});
}
functions[0]();
functions[1]();


You think you've created two variables "x", each one being local to the "for" block. But that's not the case. You have one variable x (belonging to the root level in this case).

You can get around this by introducing a new scope with a function:
Code:
var functions = [];
for (var i = 0; i < 2; i++) {
	(function (j) {		// create a new function, call it and pass i
		var x = j;		// now x is really a local variable
		functions.push(function () {
			alert(x);
		});
	})(i);
}
functions[0]();
functions[1]();

Reply With Quote
  #4  
Old November 20th, 2012, 06:45 AM
itborg itborg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 itborg User rank is Sergeant (500 - 2000 Reputation Level)itborg User rank is Sergeant (500 - 2000 Reputation Level)itborg User rank is Sergeant (500 - 2000 Reputation Level)itborg User rank is Sergeant (500 - 2000 Reputation Level)itborg User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 41 m 46 sec
Reputation Power: 0
Hii Jacques1

imgObj is Declared every time in loop.
So When the images are loading the imgObj on -
context.drawImage(imgObj, wehey, goooo);
is always the last one.

change this line to -
context.drawImage(this, wehey, goooo);

By the way - you are loosing the first image when you doing this :
i = i + 1;
imgObj.src = imgs[i];

because imgs[0] is never used...

Hope that help
Comments on this post
Winters agrees!

Reply With Quote
  #5  
Old December 2nd, 2012, 07:19 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,831 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 19 h 19 m 2 sec
Reputation Power: 4192
Quote:
Originally Posted by Grandum
Leaving this here for posterity: I didn't actually have to resort to a new function (!!!) all I had to do was what itborg said and change

context.drawImage(imgObj, wehey, goooo);
to
context.drawImage(this, wehey, goooo);

Big thanks, guys truely appreciate the help. I was way to focused on adding another function to the mix, that I forgot about that part.

Welcome to DevShed Forums, Grandum.

Next time please say that in a new reply instead of just editing the thread's first post.
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > [Solved] Closure inside a loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap