Commit eb3e8f75 authored by Michał Woźniak's avatar Michał Woźniak
Browse files

a timeout before we actually hit the Intertubes for country data (in case the...

a timeout before we actually hit the Intertubes for country data (in case the user is browsing through the list)
parent 6c131b0e
Pipeline #77 passed with stage
in 0 seconds
......@@ -224,6 +224,9 @@ let getCountries = () => {
}
/*
* get the global pandemic data
*/
let getCovidData = () => {
console.log(`+-- fetching global covid data`)
return wtf
......@@ -360,35 +363,44 @@ document.addEventListener('DOMContentLoaded', (e)=>{
}
// handle user interaction
var countrySelectTimeout = false
var countryData = document.getElementById("country-data");
countrySelect.addEventListener('change', (e) => {
if (countrySelectTimeout !== false) {
clearTimeout(countrySelectTimeout)
countrySelectTimeout = false
}
var country = event.target.value;
var countryData = document.getElementById("country-data");
console.log(`+-- fetching data for: ${country}`)
countryData.innerHTML = `<div class="please-wait">Fetching data, please wait...</div>`
getCountryCases(country)
.then((data)=>{
var ratio = data[data.length-1].cases / data[data.length-2].cases
var cases = data[data.length-1].cases
console.log(`+-- covid data for: ${country}`
+ `\n date : ${data[data.length-1].date}`
+ `\n cases : ${cases}`
+ `\n cases day prior : ${data[data.length-2].cases}`
+ `\n ratio : ${ratio}`)
getCountryPopulation(country)
.then((population)=>{
countryData.innerHTML = `
<div><span class="label">Population: </span><span class="value">${population}</span></div>
<div><span class="label">Cases: </span><span class="value">${cases}</span></div>
<div><span class="label">Infected as percent of population: </span><span class="value">${Math.round((cases/population)*10000)/100}%</span></div>
<div><span class="label">Day to day infection rate: </span><span class="value">${Math.round((ratio - 1) * 10000) / 100}%</span></div>
<div><span class="label">Cases double every: </span><span class="value">${Math.round(Math.log(2)/Math.log(ratio))} days</span></div>
<div><span class="label">Half of population infected in: </span><span class="value">${Math.round(Math.log(population*0.5/cases)/Math.log(ratio))} days</span></div>
<div><span class="label">90% of population infected in: </span><span class="value">${Math.round(Math.log(population*0.90/cases)/Math.log(ratio))} days</span></div>`
})
})
.catch((e)=>{
countryData.innerHTML = `<div class="error">Something went wrong. Either a bug, or no data.</div>`
})
countrySelectTimeout = setTimeout(function(){
countrySelectTimeout = false
console.log(`+-- fetching data for: ${country}`)
countryData.innerHTML = `<div class="please-wait">Fetching data, please wait...</div>`
getCountryCases(country)
.then((data)=>{
var ratio = data[data.length-1].cases / data[data.length-2].cases
var cases = data[data.length-1].cases
console.log(`+-- covid data for: ${country}`
+ `\n date : ${data[data.length-1].date}`
+ `\n cases : ${cases}`
+ `\n cases day prior : ${data[data.length-2].cases}`
+ `\n ratio : ${ratio}`)
getCountryPopulation(country)
.then((population)=>{
countryData.innerHTML = `
<div><span class="label">Population: </span><span class="value">${population}</span></div>
<div><span class="label">Cases: </span><span class="value">${cases}</span></div>
<div><span class="label">Infected as percent of population: </span><span class="value">${Math.round((cases/population)*10000)/100}%</span></div>
<div><span class="label">Day to day infection rate: </span><span class="value">${Math.round((ratio - 1) * 10000) / 100}%</span></div>
<div><span class="label">Cases double every: </span><span class="value">${Math.round(Math.log(2)/Math.log(ratio))} days</span></div>
<div><span class="label">Half of population infected in: </span><span class="value">${Math.round(Math.log(population*0.5/cases)/Math.log(ratio))} days</span></div>
<div><span class="label">90% of population infected in: </span><span class="value">${Math.round(Math.log(population*0.90/cases)/Math.log(ratio))} days</span></div>`
})
})
.catch((e)=>{
countryData.innerHTML = `<div class="error">Something went wrong. Either a bug, or no data.</div>`
})
}, 500)
});
// fill out the covid data box
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment