View Single Post
Old 11-18-2012, 12:06 AM   PM User | #10
Luke7268
New Coder

 
Join Date: Nov 2012
Posts: 10
Thanks: 6
Thanked 0 Times in 0 Posts
Luke7268 is an unknown quantity at this point
I read a bit about asynchronous script execution and functions in functions. Then I read about callback hell. I read and read, but the whole concept of functions inside functions was making me dizzy.

Until I found this page:
http://callbackhell.com/

I still didn't understand half of it, but one line hit me: "Write small modules that each do one thing, and assemble them into other modules that do a bigger thing. You can't get into callback hell if you don't go there."

Then I came up with this:
Code:
var loadPictureOnCanvas = function (index, url, sourcepointer, widthpic, heightcanvas) {
	img[index] = new Image();
	img[index].onload = function() {
 		ctxsrc.drawImage(img[index], sourcepointer, 0, widthpic, heightcanvas);
	};
	img[index].src = url;
}

for (i=0; i<numberofpics; i++) {
	loadPictureOnCanvas(i, imgsrc[i], sourcepointer, widthpic[i], heightcanvas);
	sourcepointer=sourcepointer+widthpic[i];
}
It works like a charm, and is so much easier to understand than functions inside functions, a concept which I really can't get my head around yet, especially not when people start nesting them infinitely.
Luke7268 is offline   Reply With Quote