Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I expect here the problem is your getComments() function (for which more detail is required). You're supplying a third argument being a callback but does the function actually use a callback? What is it doing?</p> <p>Certain jQuery functions provide callbacks but this isn't an automatic feature. If you're waiting for a user to type a comment you need to trigger the relevant event when they click "Done" or whatever they do.</p> <p>Ok, try this:</p> <pre><code>function get_comments(type, id, callback) { $.getJSON("/ajax/"+type+"/comments?jsoncallback=&amp;id="+id, function(data) { for (var x = 0; x &lt; data.length; x++) { var div = $("&lt;div&gt;").addClass("comment").appendTo("#commentList"); var fieldset = $("&lt;fieldset&gt;"); var legend = $("&lt;legend&gt;").addClass("commentHeader"); if ( data[x].url == "" ) { legend.text((x+1) + ' - ' + data[x].name); } else { $("&lt;a&gt;").attr({href: data[x].url}).text((x+1) + ' - ' + data[x].name).appendTo(legend); } legend.appendTo(fieldset); $("&lt;div&gt;").addClass("date").text(data[x].timestamp).appendTo(fieldset); $("&lt;p&gt;").addClass("comment").text(data[x].content).appendTo(fieldset); fieldset.appendTo(div); if (typeof callback != 'undefined') { callback(); } } }); } </code></pre> <p><strong>Note:</strong> the difference here is that a third argument is supplied to get_comments() which is a callback that'll be called at the end of your $.getJSON() callback. That'll give you the proper ordering you want.</p> <p>I might also suggest not constructing the HTML like that but including it in your page and hiding/unhiding it as necessary. It tends to be much more performant that dynamic HTML and have less issues (eg new HTML, unless you use $().live() will not have relevant event handlers).</p> <p><strong>Edit:</strong> Made the callback optional as per the comments. With the above code you can call the function without or without the callback.</p>
 

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