Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Canvas, setTimeout, recursion problems...

    Well, I clearly haven't the experience with javascript to understand why I can't do this:

    Code:
    function bubbleSortKernel(a, b) {
    	if (b < 50) {
    		swap(a, b);
    		draw();
    		setTimeout("bubbleSortKernel(++a, ++b)", 200);
    	}
    }
    where
    Code:
    swap(a, b)
    does just what it says: swaps the two elements in an array, and
    Code:
    draw()
    looks about like this:

    Code:
    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!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •