I have Ajax function with a callback which fetches all the Patient data however I have some perfomance issues and trying to figure out what it might be, does anyone have idea? That's my Code
var patient = new Array();
function sync(arg, callback){ //ajax result
$('.loader').show();
$.ajax({
method: 'GET',
url: 'sync/active.php',
dataType: 'json',
data: arg, // argument schicken und aktualisieren
success: function(data, status, xhr){
$('.loader').hide();
callback(data);
// setTimeout(sync, ajaxDelay);
},
error: function(xhr, ajaxOptions, thrownError){
console.log(thrownError);
}
});
}
function onPatientCallback(data) {
var res = data;
for(var i=0; i<res.length;i++){
for(var key in res[i]){
var value = res[i][key];
switch(key){
case "id":
res[i][key] = parseInt(value);
break;
case "kundennr":
res[i][key] = parseInt(value);
break;
case "client":
res[i][key] = value;
break;
case "start":
res[i][key] = new Date(value);
break;
case "end":
res[i][key] = new Date(value);
break;
case "title":
res[i][key] = value;
break;
case "description":
res[i][key] = value;
break;
case "termart":
res[i][key] = parseInt(value);
break;
case "userId":
res[i][key] = parseInt(value);
break;
default:
console.log("unbekannter Datentyp "+key);
}
}
}
patient = res;
}
I use the function to fill the patient variable and call it in another js file like that sync({calling: "patient"}, onPatientCallback);
1 Answer 1
From a short review;
patient
is a global variable, global variables are badvar patient = []
is more idiomatic thanvar patient = new Array();
- Comments should be all German or all English (I would go for all English, its the common language of the developers)
You can group
switch
labels, this is valid JavaScript:switch(key){ case "id": case "kundennr": case "termart": case "userId": res[i][key] = parseInt(value); break; case "client": case "title": case "description": res[i][key] = value; break; case "start": case "end": res[i][key] = new Date(value); break; default: console.log("unbekannter Datentyp " + key);
- Your data fields should be either all English or all German (you have
userId
, but alsokundennr
), here as well I would go for all English - Since
value
already containsres[i][key]
for "client", "title", etc., you should probably comment on that - Since this is real, you should not log to console for an unknown data type, you should call a REST service that will log this entry in a database table, and even possibly send out an email to developers
var key in Object.keys(res[i])
is safer thanvar key in res[i]
, you never know when someone decides to extendObject
- Your indenting is off, consider using a beautifier
Explore related questions
See similar questions with these tags.
snyc()
andonPatientCallback()
are called, and 2. possible HTML corresponding to this code? \$\endgroup\$