View Full Version : Canvas, setTimeout, recursion problems...

12-17-2011, 02:18 AM
Well, I clearly haven't the experience with javascript to understand why I can't do this:

function bubbleSortKernel(a, b) {
if (b < 50) {
swap(a, b);
setTimeout("bubbleSortKernel(++a, ++b)", 200);

swap(a, b) does just what it says: swaps the two elements in an array, and
draw() looks about like this:

function draw() {
if (ctx != null) {
ctx.clearRect(0, 0, width, height);
for (var i = 0; i < rectangles.length; i++) {
ctx.fillRect(x, rectangles[i][0], rectangles[i][1], rectangles[i][2]);

I'm newer to javascript that other things, and this seems perfectly legal to me. I'm looking for someone who knows more that I and can explain what I'm missing. I'm guessing my timeout doesn't really work like I think it does, but I've gotten so frustrated I can't see the forest for the trees.

Any help or nudge is greatly appreciated. Thanks!

Old Pedant
12-17-2011, 03:06 AM
You don't show the code for the swap( ) function. Nor do you show the code that calls bubbleSortKernel.

In any case, the name of your function, "bubbleSortKernel", if pretty darned misleading, because there is no way in the world it is actually doing anything resembling a bubble sort.