I believe my issue may be to do with the length of time a function is taking to refresh the list but 2 days of trying everything a dozen different google searches can find has left me stuck...
I am creating a list dynamically:
<ul id="events" class="select" name="events" style="width:40vw;"></ul>
LoadEvents()
{
var xhttp;
var EventList;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
EventList = this.responseText.split(",");
//Clear List
$('#events').empty();
EventList.forEach(function(item, index, array)
{
if(item !== "")
{
$ID = item.split(";")[0];
$('#events').append('<li class="event" id=\"' + $ID '\">' + item.split(";")[1] + '</li>');
}
});
}
};
xhttp.open("GET", "DataLink2.php?con=GetFullEvents&orderby=" + $OrderBy, true);
xhttp.send();
}
EventList
is an array of strings "ID;EventName" drawn from a database.
To extract the ID I am using:
var selectedid;
//Within the function that requests the refresh I do
selectedid = $('#events').find('li.selected').attr('id');
//calling the refresh function
LoadEvents();
//And trying to reselect the item in the list
$('#' + selectedid).click();
On a fiddle it works perfectly but in reality it always selects the item before refreshing the list!
I have tried various ideas from similar questions on here including:
$.when(LoadEvents()).then($('#' + selectedid).click());
and I also tried the above putting $('#' + selectedid).click();
into a function.
I then tried the first two solutions offered on this question (haven't quite worked out how to do the third option with the "myCustomevent" yet) but in every case the $('#' + selectedid).click();
was performed before the li's were re-generated.
Is there a way to force one event to wait until another has completely finished?
php:
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if(mysqli_connect_errno())
{
$query = "SELECT * FROM `Events` WHERE `FinishTime` > '00:00:00' ORDER BY `FinishTime` DESC";
//Query Result
if ($result = mysqli_query($connect, $query))
{
/* fetch associative array */
while ($obj = mysqli_fetch_object($result))
{
printf ("%s,", $obj->ID.";".$obj->EventName;
}
/* free result set */
mysqli_free_result($result);
}
else
{
echo '<p>Connection failed.</p>';
}
}