Cross Cross - 1 year ago 51
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?

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download