Home » Php » php – Javascript performance script

php – Javascript performance script

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have 2 Javascripts scripts for 2 purposes. One of them works like a charm, the other, turns my browser slow and hard tou use.

Both scripts make a HTTP request to a PHP file on a remote server every X seconds.

  1. One script receive values and displays them – This one works fine evene every second (the browser doesn’t slow down)
  2. The other receive values (1 or 0) to hide or show small images acording to the state, 0 or 1.

—- The script that receives values and is OK —-

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
 var refreshId = setInterval(function() { $( "#var_3" ).load( "getvalue.php?var=var_3.VALUE&type=0&round=1&dec=1" ); }, 1000);
 var refreshId = setInterval(function() { $( "#var_4" ).load( "getvalue.php?var=var_4.VALUE&type=0&round=1&dec=1" ); }, 1000); 
 var refreshId = setInterval(function() { $( "#var_5" ).load( "getvalue.php?var=var_5.VALUE&type=0&round=1&dec=1" ); }, 1000);
 var refreshId = setInterval(function() { $( "#var_0" ).load( "getvalue.php?var=var_0.VALUE&type=0&round=1&dec=1" ); }, 1000);
 var refreshId = setInterval(function() { $( "#var_1" ).load( "getvalue.php?var=var_1.VALUE&type=0&round=1&dec=1" ); }, 1000);
</script>

<span id="var_3">

Answer:

</span> <span id="var_4">

Answer:

</span> <span id="var_5">

Answer:

</span> <span id="var_0">

Answer:

</span> <span id="var_1">

Answer:

</span>

Now the script that makes my browser suffer and I can’t almost use the browser:

<script>
function GetEstado(para1) {

jQuery.extend
(
   {
       getValues: function(url) 
       {
           var result = null;
           $.ajax(
               {
                   url: url,
                   type: 'get',
                   dataType: 'html',
                   async: false,
                   cache: false,
                   success: function(data) 
                   {
                       result = data;
                   }
               }
           );
          return result;
       }
   }
);

   VARZ = $.getValues("getvalue.php?var="+para1+"&type=1");
   if (VARZ == 1) {
    document.getElementById(para1).style.display = "inline";
   } else {
    document.getElementById(para1).style.display = "none";
   }
};



setInterval(function() { GetEstado("OUT_39"); }, 5000);
setInterval(function() { GetEstado("OUT_31"); }, 5000);
setInterval(function() { GetEstado("OUT_40"); }, 5000);

HTML
<div id="OUT_39" style="display: inline"><img src="img/valv1.png" style="position: absolute; top: 544px; left: 397px;"></div>
<div id="OUT_41" style="display: inline"><img src="img/valv1.png" style="position: absolute; top: 244px; left: 297px;"></div>
<div id="OUT_40" style="display: inline"><img src="img/valv1.png" style="position: absolute; top: 44px; left: 17px;"></div>

Is there any way to make this script diferent and more faster like the first one?

How to&Answers: