Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/11919147/need-help-fixing-bugs-and-optimizing-javascript-code">Need help fixing bugs and optimizing javascript code</a></p> <p>I am going to give you some general advice. In general it is better to get something working before you worry about something being perfectly efficient. That doesn't mean you should purposely do things in a sloppy inefficient way but correctness should be more important than something being optimized.</p> <p>You can go back through your code and optimize it after you have it working. I have found that if I focus too hard on making something efficient ( before I complete it ) I will never get it done!</p> <p>As far as making your code better a good rule of thumb is to cache things. </p> <p><strong>For example do:</strong></p> <pre><code>var container = $('#container'); container.css({ ‘background-color’ : ‘#f00’}); container.bind(‘click’, function() { do something }); </code></pre> <p><strong>Instead of:</strong></p> <pre><code>$(‘#container’).css({‘background-color’ : ‘#f00’}); $(‘#container’).bind(‘click’, function() { do something }); </code></pre> <p>Everytime you do $(‘someElement’) it takes time. It has to search the entire DOM to find that element. If you are going to be referencing this DOM element often it is better to save a reference to this element and use this reference. Thus removing all these unnecessary lookups.</p> <p>Another thing to think about using is jquery’s delegates when possible. Delegates are good if you have a lot of click listeners inside the same area. </p> <p>For example rather then adding a click listener on all the links in your .locBox class:</p> <pre><code>$('.locBox a').click(function (e) { </code></pre> <p><strong>Do something like this:</strong></p> <pre><code>$(‘.locBox’).delegate(‘a’, ‘click’, function() { … do something … }); </code></pre> <p>This is good because now you will only have one listener on each .locBox and it will wait for a click on your link tags to bubble up then run the event handler.</p> <p>I know this doesn’t address your “buggy” behaviour but I think you will find this useful for future projects.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload