Cross Cross - 1 month ago 5
PHP Question

What is the proper way of using a javascript, ajax ? I am getting CPU reach limit on my site

Maybe i think this script (code snippet) below is causing me High server load make my site down as well as CPU reach limit. Is there a proper way to code the variables and arragement of this script?

One point here also, I am using here a SetTimeout(); because i REALLY NEED to get data from a JSON file from PHP and MYSQL every 2 seconds any other way to set this in proper way?, What can i do minimize my server load and avoid reaching the limit of the cpu of my web host?


There is no use of $(document).ready(function () {...} inside the locate function

Also you have place the setTimeout outside the function locate

function locate() {
  $.get(get_loc + v_id, function(data) {
    var obj = JSON.parse(data)
    longtitude = obj.vehicle[0].longtitude;
    latitude = obj.vehicle[0].latitude;
    if (obj.vehicle[0].longtitude == "") {
      document.getElementById("coordinates").innerHTML = "No coordinates found in the database";
    } else {
      document.getElementById("coordinates").innerHTML = "Longtitude: " + longtitude + "<br>Latitude: " + latitude + " ";
setTimeout(locate, 2000);