I mainly develop in PHP and every now and again I have to do stuff in JavaScript which I'm not really comfortable, so I would like a review of how I can improve my code and knowledge. This script takes data coming from JSON, does some basic checking on the data fro format and things like that and then displays the data.
The code also calls a function in PHP that deals with updating the data in the JSON file, and then repeats the process.
$(document).ready(
function () {
setInterval(function () {
console.log("start");
$.ajax({url: "/QcT/bulk"})
.done(function (data) {
console.log("Done");
$.getJSON("../../../UpdatedData/QC/QC.json", function (data) {
var div = document.getElementById('Store');
div.innerHTML = "";
var i = 0;
for (var i = 0; i < data.length; i++) {
var Machine = data[i];
//Correctley format the machine name
if (Machine.MachineNo < 10) {
MachineName = "ZW0100" + Machine.MachineNo;
} else {
MachineName = "ZW010" + Machine.MachineNo;
}
// Test to see if we have a first off.
console.log("Machine" + MachineName + "override " + Machine.FirstoffOverride + " FirstOFfTime " + Machine.FirstoffTime);
if (Machine.FirstoffTime !== null) {
Firstoff = "<div class='col-md-1' style='border-color: black;padding-left: 0px;margin-right: 2px;border-style: solid;border-width: 2px;-webkit-border-radius: 27px;-moz-border-radius: 27px;border-radius: 27px;padding-right: 0px;background-color: greenyellow;font-size: 30px;text-align: center;width: 46px;margin-left: 10px;color:greenyellow;' > 1 </div>";
} else {
Firstoff = "<div class='col-md-1' style='visibility: hidden; padding-left: 0px;margin-right: 2px;border-style: solid;border-width: 2px;-webkit-border-radius: 27px;-moz-border-radius: 27px;border-radius: 27px;padding-right: 0px;background-color: ##DFDFDF;font-size: 30px;text-align: center;width: 46px;margin-left: 10px;color:#DFDFDF;' > 1 </div>";
}
//Test to see if a comment is set
if (Machine.comment !== null) {
comment = Machine.comment;
} else {
comment = " ";
}
var color = "";
var time = "";
content = "<div class='panel panel-default' style='font-weight: 700;margin-bottom:0px;border-radius: 46px;background-color: #DFDFDF ;box-shadow: 3px 5px 14px #474747;height:63px;margin-bottom: 11px;' >";
content += "<div class='panel-body' style='padding: 10px;'>";
content += "<div class='col-md-8'>";
content += "<div class='col-md-2' style='font-size: 33px;color:black'>" + MachineName + "</div>";
content += Firstoff;
content += "<div class='col-md-9 text-center' style='font-size: 28px;color:Black;'>" + comment + "</div></div>";
content += "<div class='col-md-4'>";
$.each(Machine.PassFail, function (key, value) {
//Check to see if it's pass or fail
if (value.password === '0001') {
color = "greenyellow";
time = value.ReaderTime;
} else {
color = "red";
time = value.ReaderTime;
}
content += "<div class='col-md-1' style='padding-left:0px;margin-right: 2px;border-style: solid;border-width: 2px;-webkit-border-radius: 27px;-moz-border-radius: 27px;border-radius: 27px;padding-right:0px;background-color:" + color + ";font-size: 30px;text-align: center;' >" + time + "</div>";
});
content += "</div></div></div></div>";
$(content).appendTo(div);
}
;
console.log("refresh");
});
});
}, 6000);
});
The data from the Ajax call runs the script that updates the JSON File. It doesn't directly interact with the data, but just updates the JSON file:
/../../UpdatedData/QC/QC.json