Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>JavaScript behaves in an asynchronous manner, meaning it <strong>does not wait</strong>.</p> <p>You have a part in your code that looks like this:</p> <pre><code>jQuery.ajax({ url: "http://"+domain+"/_ajax/"+requestURL, dataType: "html", async: true, data: data, type: type, success: function(msg){ console.log(msg); msgResponse = msg; msgRecieved = true; } }); </code></pre> <p>You should place the code to be run when the response arrives within the <code>success</code> function, like so:</p> <pre><code>success : function (msg) { handleMessage(msg); // Or any other manipulation to the received message } function handleMessage(msg) { // Work with your received message here. } </code></pre> <p><code>success</code> will be called with the <em>received</em> message, it is a callback.</p> <p>The right way to implement <code>sendMessage</code> would be the following way:</p> <pre><code>sendMessage = function( requestURL, data, type, callback ) { console.log(requestURL); console.log(data); console.log(type); //reset vars to defaults msgRecieved = false; msgResponse = null; jQuery.ajax({ url: "http://"+domain+"/_ajax/"+requestURL, dataType: "html", async: true, data: data, type: type, success: function(msg){ console.log(msg); msgResponse = msg; msgRecieved = true; // Call the callback function to notify the message // was received callback(); } }); }; </code></pre> <p>and then using it like so:</p> <pre><code>sendMessage(urlHere, dataHere, typeHere, function () { // Message has been received, msgResponse and msgReceived // have already been updated. Do what you need here }); </code></pre>
    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.
 

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