Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass responseText from asynchronous AJAX call to another function?
    primarykey
    data
    text
    <p>I'm using jQuery and <a href="http://keith-wood.name/countdown.html" rel="nofollow noreferrer">countdown</a> (jQuery plugin). I want the countdown to run for x seconds, where x is an integer from serverTime.php.</p> <p>Problem - I can't figure out how to return the responseText in function getTime(), so that it could be used in Timer function.</p> <p>At first I tried using pure js (without jQuery), and I found similar topics:</p> <ul> <li><a href="https://stackoverflow.com/questions/3732258/why-can-i-not-return-responsetext-from-an-ajax-function">why-can-i-not-return-responsetext-from-an-ajax-function</a></li> <li><a href="https://stackoverflow.com/questions/290214/in-ajax-how-to-retrive-variable-from-inside-of-onreadystatechange-function">in-ajax-how-to-retrive-variable-from-inside-of-onreadystatechange-function</a></li> <li><a href="https://stackoverflow.com/questions/1955248/how-to-return-variable-from-the-function-called-by-onreadystatechange-function">how-to-return-variable-from-the-function-called-by-onreadystatechange-function</a></li> <li>and others...</li> </ul> <p>Some ideas/possible solutions I've come across:</p> <ul> <li>using synchronous call and a global variable for storing response. I tried this and don't consider it an option, because it freezes the user screen (and is considered bad practice as well, in most cases)</li> <li><a href="https://stackoverflow.com/questions/3732258/why-can-i-not-return-responsetext-from-an-ajax-function/3732268#3732268">helper function</a> .</li> - </ul> <p><strong>Timer function</strong>:</p> <pre><code>$(function (){ var time = getTime(); // time is an integer $('#defaultCountdown').countdown({until: time, format: 'S', onExpiry: action}); }); </code></pre> <p><strong>getTime()</strong> checks serverTime.php and should return the content, which will be an integer. Alert(html) works, while return html does not work. I know it's because the call is asynchronous.</p> <pre><code>function getTime() { $.ajax({ url: "serverTime.php", cache: false, async: true, success: function(html) { alert(html); helperFunction(html); return html; } }); } </code></pre> <p><strong>helperFunction</strong>. Unfortunately, I don't know how to pass the result to timer function.</p> <pre><code>function helperFunction(response) { return response; } </code></pre> <p><strong>serverTime.php</strong></p> <pre><code>echo 30; // 30 seconds, just an example </code></pre> <p>I've tried to explain my problem and show that I've put some time into it. If you need additional information, just ask. Maybe I'm just thinking in the wrong direction.</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.
 

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