Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #19433 > unrolled thread
| Started by | "Tony" <tony.johansson@inport.com> |
|---|---|
| First post | 2013-04-24 15:12 +0200 |
| Last post | 2013-04-25 01:49 +0700 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.javascript
Why can't I wait for a flag that is set in an synchronous function "Tony" <tony.johansson@inport.com> - 2013-04-24 15:12 +0200
Re: Why can't I wait for a flag that is set in an synchronous function JJ <duh@nah.meh> - 2013-04-25 01:49 +0700
| From | "Tony" <tony.johansson@inport.com> |
|---|---|
| Date | 2013-04-24 15:12 +0200 |
| Subject | Why can't I wait for a flag that is set in an synchronous function |
| Message-ID | <kl8lib$509$1@dont-email.me> |
The function GetBooking() is using asynchronous communication to get data
from the database.
I want to wait by using the while loop until the ready flag is set in the
GetBooking function to true and then continue with the array that was
loaded in the
GetBooking().
I use setInterval to call a function CheckUntilAsyncComplete that will check
if the ready flag has been set to true.
This works only if I add an alert to the while loop.
It seems to me that if I don't have an alert It will execute and never call
the CheckUntilAsyncComplete
$(function () //wait until the DOM writing is complete
{
GetBooking();
status = setInterval(CheckUntilAsyncComplete()', 1000);
while (!ready)
{
//alert("Looping");
}
//Here I want to use the array that is filled in GetBooking()
});
function CheckUntilAsyncComplete()
{
if (ready)
{
clearInterval(status);
}
}
//Tony
[toc] | [next] | [standalone]
| From | JJ <duh@nah.meh> |
|---|---|
| Date | 2013-04-25 01:49 +0700 |
| Message-ID | <10woqcm5fo6qt.hm1pw14yka12.dlg@40tude.net> |
| In reply to | #19433 |
On Wed, 24 Apr 2013 15:12:38 +0200, Tony wrote:
> The function GetBooking() is using asynchronous communication to get data
> from the database.
> I want to wait by using the while loop until the ready flag is set in the
> GetBooking function to true and then continue with the array that was
> loaded in the
> GetBooking().
>
> I use setInterval to call a function CheckUntilAsyncComplete that will check
> if the ready flag has been set to true.
> This works only if I add an alert to the while loop.
> It seems to me that if I don't have an alert It will execute and never call
> the CheckUntilAsyncComplete
>
> $(function () //wait until the DOM writing is complete
> {
> GetBooking();
> status = setInterval(CheckUntilAsyncComplete()', 1000);
>
> while (!ready)
> {
> //alert("Looping");
> }
>
> //Here I want to use the array that is filled in GetBooking()
> });
>
> function CheckUntilAsyncComplete()
> {
> if (ready)
> {
> clearInterval(status);
> }
> }
>
> //Tony
You need to remember that JavaScript is always run single-threaded even if
you have 99-cores CPU.
The reason it works when you insert the "alert" function in that loop is
that, the execution will go into alert's code, waiting for user input. While
it's waiting, the execution goes into idle state, and may allow some events
to fire.
You'll need to refactor your code to allow the idle state when checking the
"ready" variable. e.g.:
(function ()
{
var status;
function CheckUntilAsyncComplete()
{
if (ready)
{
clearInterval(status);
//Here you use the array that is filled in GetBooking()
}
}
GetBooking();
status = setInterval(CheckUntilAsyncComplete(), 1000);
//exit all functions to go into idle state
})();
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web