Let me just preface by saying it’s actually my crappy code that’s leaking and crashing my browser, I just thought I better make the languages being used as clear as I could from the outset.
I have a test page here and the javascript can be found here. My problem is that when I try and drag and drop either one of the red pieces more than a few times it sucks up all browser resources and crashes the browser. I’m fairly certain the culprit is something in the following function in the Tracker() object but I’m absolutely stuck on how to debug this.
My current most likely culprit:
function register_draggable(ob) { ob.config.jqId.draggable({cursor: 'move', grid:[ob.config.size, ob.config.size], containment: '#chessboard', revert: 'invalid', start: function() { check_allowable_moves(ob.config.jqLocation, ob.config.jqId, ob); }, stop: function() { remove_allowable_moves(); } }); }
If anyone could take a quick look and give me any suggestions on what I should be looking for, it would be enormously appreciated.
Solution Turns out register_draggable() was the culprit. I registered a new draggable every time the location of a piece updated and all those draggables on the same object were doing nasty things. Currently I now explicity destroy the old draggable before creating a new one. Current code is
function register_draggable(ob) { ob.config.jqId.draggable('destroy'); ob.config.jqId.draggable({cursor: 'move', grid:[ob.config.size, ob.config.size], containment: '#chessboard', revert: 'invalid', start: function() { check_allowable_moves(ob.config.jqLocation, ob.config.jqId, ob); }, stop: function() { remove_allowable_moves(); } }); }
I don’t think this is actually your problem, but it seems like your making an extra method call on register and check_ allowable_moves
can be shortened to
Also
you are doing a double lookup here:
should be
Also
Whats the purpose of parsing and int into a float? Take off the parseFloat.
Finally
Why are you re-registering as draggable on drop? This could be the culprit, if your registering the draggable multiple times and only destroying it once.
Other thoughts
Another thing I don’t know if its going to help your performance, but it could clean up your code. the jquery selector allows commas so instead of
you could do
so your loop would only be concerned with generating the selector string and then you could run you operation on the entire set.
IMO a cleaner init
instead of
I’d like to see
Now implementing that is a strech from what you have now, but when building a component for jQuery I like to start with a simple init and work from their. Thats one of the big goals of jQuery is to hide all the junk from the user and just let them spin up and instance of your plugin easily.