I would like to display startups from the Crunchbase API (REST API) via a URL with PHP. To retrieve the json data I am using Ajax to provide a asynchronous request.
The first function ajaxLoad(page, search);
will do the first xmlhttp
- XMLHttpRequest() to get the "permalink" - var permalink = json_de.data.items[c].properties.permalink;
that is used by the second xmlhttp2
- XMLHttpRequest() which is NOT asynchronous. The second request will provide further information about the specific startup (var permalink = "kickstarter"
for example). I only want to display startups which are founded after 2000-01-01 (unixtime >= 946684800
). My current code works but I would like that both request are asynchronous.
How do I create two asynchronous requests? Or is there a more elegant way to carry out this task? Currently the loading time is VERY high.
It takes about 1 minute to loop through 20 startups. Could that be done faster? Thanks for the help.
function ajaxLoad(page, search) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { <?php
if (isset($_GET['company'])) { ?>
makeCompanyTable(xmlhttp.responseText); <?php
} else { ?>
makeATable(xmlhttp.responseText); <?php
} ?>
}
}
xmlhttp.open("GET", "get_json.php?p=" + page + "&search=" + search + "&company=<?php echo $companyvar; ?>", true);
xmlhttp.send(); }
function makeATable(json) {
var json_de = JSON.parse(json);
var count = Object.keys(json_de.data.items).length
var c = 0;
var val = "";
var col_counter = 1;
while (c != count) {
var xmlhttp2 = new XMLHttpRequest();
var permalink = json_de.data.items[c].properties.permalink;
xmlhttp2.open("GET", "get_org.php?permalink="+ permalink, false);
xmlhttp2.send();
var json_com = JSON.parse(xmlhttp2.responseText); // Ganze info des jeweiligen Startup
var founding_date = json_com.data.properties.founded_on;
var unixtime = Date.parse(founding_date)/1000;
if (unixtime >= 946684800) {
val = val + '<div class="card profile-view"><div class="pv-header"><img src="';
var cln = "";
cln = cln + json_de.data.items[c].properties.profile_image_url;
if (cln == "" || cln.length <= 0) {
val = val + "img/does_not_exist.png";
} else {
val = val + json_de.data.items[c].properties.profile_image_url;
}
val = val + '" class="pv-main" alt=""></div><div class="pv-body"><h2>';
val = val + json_de.data.items[c].properties.name;
cl = json_de.data.items[c].properties.short_description;
val = val + '</h2><small';
val = val + '>';
val = val + json_de.data.items[c].properties.short_description;
val = val + '</small><a href="startup.php?company=' + json_de.data.items[c].properties.permalink + '" class="pv-follow-btn">Anzeigen</a></div></div>';
//alert(val);
document.getElementById("col_"+col_counter).innerHTML += val;
val = "";
col_counter++;
if(col_counter == 7){
col_counter = 1;
}
}
c++;
} }