Skip to content

Instantly share code, notes, and snippets.

@AsadR
Created October 23, 2010 23:22
Show Gist options
  • Save AsadR/642818 to your computer and use it in GitHub Desktop.
Save AsadR/642818 to your computer and use it in GitHub Desktop.
That anonymous function won't get the value of "i". Naughty little bugger.
<canvas id="e" width="800" height="600"></canvas>
<script>
var canvas = document.getElementById("e");
var context = canvas.getContext("2d");
var images = [];
var k = 0;
for( i = 0; i < 10; i++ ) {
var cat = new Image();
cat.src = "closed.png";
images[i] = cat;
cat.onload = function() {
k++;
// assuming 10 images have to be loaded
if( k < 9 )
return;
// control reaches here when all the images have been loaded
for( i = 0; i < 10; i++ ) {
var cat = images[i];
context.drawImage(cat, i * 100, 0);
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment