0

I have never seen anything like this, and not sure why / what is going on. I am coming from a desktop coding background, it feels like a multi-thread is happening.

The code and output works fine, if I put an alert in, to check my returns, and make sure the data from the PHP code is correct, and that it is returning from the function.

If I let it run at "full speed", no alerts for checking, it is not displaying the data that gets returned, but will show all other HTML being built in the same function.

The code flow is like this, the HTML page builds a basic table, and I am using the document ready to trigger my main java file. Inside the Java code, I am looping through a date range with a "while" (while currentdate <= enddate).

Inside the while loop, I am sending the date to another function, which then, calls the PHP file, to grab data for the date, build the HTML for display, then return that, to the while loop to display on the page.

Here is where it feel like it is creating a second thread. It is like the Java code is not waiting for the data from PHP, and keeps going, since it will output the "day", that is not part of the PHP return.

In the code below, if I were to uncomment any of the alerts, it will show not only the data from PHP, but will display on the page, let it run at full speed, all it get is the HTML that displays the day.

function Get_Events(Year_In, Month_In, Day_In)
{

    var EventList = '<table class="InnerTable">\
        <thead>\
        <tr class="DayNumber">\
        <th class="DayNumberCurrent">' + Day_In + '\
        </th>\
        </tr>\
        </thead>';

    var EventDateData = {
        'Action': 'GetEvents',
        'Month': Month_In + 1,
        'Day': Day_In,
        'Year': Year_In
    };

    $.ajax({
        type: "POST",
        url: "Scripts/DataTesting.php",
        dataType: "json",
        data: EventDateData,
        success: function(EventListing) {

            //alert(EventListing[0].EventName); 

            for (var key in EventListing) {
                alert('Has Key');
                if (EventListing.hasOwnProperty(key)) {

                    EventList += '<tr>';
                    EventList += '<td>' + EventListing[key].EventName  + '</td>';
                    EventList += '</tr>';

                }
            }

        },
        error: function (jqXHR, error, errorThrown) {
            //  alert(jqXHR.responseText);
            if (jqXHR.status) {
                alert(jqXHR.responseText);
            } else {
                alert("Non-JQ Error: " + error + ", " + errorThrown);
            }
        }
    });

    EventList += '</table>';
    //alert('HTML: ' + EventList);
    return EventList;
}
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
MaxThrust
  • 137
  • 2
  • 2
  • 9
  • 2
    Please update your title to 1. say JavaScript instead of Java (two totally different monsters). 2. Be clear what your question is all about. It's currently impossible to understand what you mean when reading it. – M. Eriksson Aug 03 '18 at 13:25
  • Magnus, I fixed the Javascript request, however, I do not know how to better form the question, since I am very confused myself. If you have a better phrasing, I will use it. – MaxThrust Aug 03 '18 at 13:32
  • 1
    I don't see a while loop, but I can hazard a guess on what the issue is ... JavaScript is natively, non-blocking. Unless you expressly do something (like callbacks or async/await or promises), a loop will continue without any care on how long an endpoint takes to retrieve data. However, alerts will pause the execution of JavaScript before continuing. So, dropping in an alert fakes a code pause and lets the endpoint catch up. – Doug Aug 03 '18 at 13:32
  • 2
    `$.ajax()` is **asynchronous**. The "success" callback will be called when the HTTP request completes, but the `$.ajax()` function call returns immediately. – Pointy Aug 03 '18 at 13:33
  • You are right, this is a multithreading issue although you can only manage one thread in js. – Jonas Wilms Aug 03 '18 at 13:42
  • Doug, you are spot on, with what I am seeing. How do I make it wait? The while loop is very basic while current <= end, do the function. – MaxThrust Aug 03 '18 at 13:58
  • If you can give me some keywords, i can do homework, just not sure what I am looking for. – MaxThrust Aug 03 '18 at 13:59
  • Thanks Jonas, that post is long, but now seeing what is going on. Now need to roll up the sleeves and do some rework =). I just did not know, what I was asking, just knew what I was seeing. – MaxThrust Aug 03 '18 at 14:39
  • I tried to drop some helpful keywords in my response, "callback", "async/await", "promise" -- they're all kind of doing the same thing, when you go through your loop, take the state of the data at that time (variables and what not) and move them into another part of the code to remember and execute when it can. If you want blocking (where the loop won't continue until the next thing) then async/await or promises may help – Doug Aug 03 '18 at 19:32

0 Answers0