JSFiddle - React, Tailwind, and code Playground
by Jerry
JavaScript
// Declaration of some global vars.
var x; var y; var z;
// Get data from database by sending values to php script.
function getData(action, value) {
$.ajax({
url: '/some/url.php',
data: { action: action, value: value },
type: 'post',
dataType: "json"
})
.done(function(response){ // Handling callback (?).
returnData(response, action);
})
.fail(function(){ // Handling callback error.
console.log("Error occured!");
});
}
function returnData(data, action) {
// Switch the actions to send output to the right variable.
switch(action) {
case 'start' :
x = data;
break;
case 'finish':
y = data;
break;
case 'line' :
z = data;
break;
}
}
// Variables that need the data.
x = getData('position', 'start');
y = getData('position', 'finish');
z = getData('line', '1');
/* Do something with variables. */