Updating Div of data from a Json file without flashing the screen

Asked

Viewed 1,394 times

2

I am using the script below to take values that are in a Json file (which is stored in an industrial automation equipment connected on a TCP/IP network and is "type" a ULTRA basic web server) and put them in Divs of a Divs page based on Div ID. The script is inside that index.html file that is stored on my PC that is also connected on the same network.

This "server" is ULTRA limited because it does not allow me to make any special configuration to avoid the cross-Omain problem, it does not run anything server-side and has very little memory (30Kb). That’s why I have to run the pages that take information from this equipment, on my PC. Basically it takes sensor information from an industrial plant and replaces the values of type ":="BD". TAG:" (of the Json file in it) in known values.

Script inside the index.html

<script>
    function callback(json)
    {
        document.getElementById("Nro_Ensaio").innerHTML = json.Nro_Ensaio;
        document.getElementById("SP_Pelotas1").innerHTML = json.SP_Pelotas;
        document.getElementById("SP_Pelotas2").innerHTML = json.SP_Pelotas;
        document.getElementById("PV_Pelotas1").innerHTML = json.PV_Pelotas;
        document.getElementById("Status").innerHTML = json.Status;
    }
</script>
<script type="text/javascript" src="http://192.168.0.103/awp/VAR_PRENSAS/ensaio.json"></script>

Essay.json

callback({
    'Inicia': ':="ENSAIO".CMDS.LIBERA:',
    'Rearme': ':="ENSAIO".CMDS.RESET:',
    'Nro_Serie': ':="ENSAIO".Nro_Serie:',
    'Modelo': ':="ENSAIO".Modelo:',
    'Nro_Ensaio': ':="ENSAIO".Nro_Ensaio:',
    'Pronto': ':="ENSAIO".Pronto:',
    'Data': ':="ENSAIO".Data:',
    'Hora': ':="ENSAIO".Hora:',
    'SP_Pelotas': ':="ENSAIO".SP_Pelotas:',
    'PV_Pelotas': ':="ENSAIO".PV_Pelotas:',
    'Status': ':="ENSAIO".Status:'
});

When I open the index.html file in any browser I can see the data received by this device, but I need these values to be updated every second. I tried to update the page with the code below, but the data I receive from the equipment keeps flashing during the page update.

<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
    setTimeout("location.reload(true);",timeoutPeriod);
}
//   -->
</script>
</head>
<body onload="JavaScript:timedRefresh(1000);">

How can I do to read this json file every second and update the data from the index.html page without "flashing" the page during the update?

  • Have you looked at this question/answer? http://answall.com/questions/6626/como-crea-um-site-sem-reloade-a-cada-clique-num-link

  • Already @Sergio, but due to limited equipment I can’t do it on it.

1 answer

1

In this case, to not "blink" the screen, you should call the javascript several times and not the location.reload().

Change the setTimeOut function to Chara Callback and place the callback call at the end of the body.

Example

HTML

<body>

<script>
    function callback(json)
    {
        document.getElementById("Nro_Ensaio").innerHTML = json.Nro_Ensaio;
        document.getElementById("SP_Pelotas1").innerHTML = json.SP_Pelotas;
        document.getElementById("SP_Pelotas2").innerHTML = json.SP_Pelotas;
        document.getElementById("PV_Pelotas1").innerHTML = json.PV_Pelotas;
        document.getElementById("Status").innerHTML = json.Status;
    }
</script>

<script type="text/javascript" src="http://192.168.0.103/awp/VAR_PRENSAS/ensaio.json"></script>
</body>

Essay.json

 setTimeout(function(){
        callback({
            'Inicia': ':="ENSAIO".CMDS.LIBERA:',
            'Rearme': ':="ENSAIO".CMDS.RESET:',
            'Nro_Serie': ':="ENSAIO".Nro_Serie:',
            'Modelo': ':="ENSAIO".Modelo:',
            'Nro_Ensaio': ':="ENSAIO".Nro_Ensaio:',
            'Pronto': ':="ENSAIO".Pronto:',
            'Data': ':="ENSAIO".Data:',
            'Hora': ':="ENSAIO".Hora:',
            'SP_Pelotas': ':="ENSAIO".SP_Pelotas:',
            'PV_Pelotas': ':="ENSAIO".PV_Pelotas:',
            'Status': ':="ENSAIO".Status:'
        });
    }, 1000);
  • Hello @Anmaia, Will setTimeout stay inside the same Essay.json? Because you put (2x) <script type="text/javascript" src="http://192.168. 0.103/Awp/VAR_PRENSAS/ensaio.json"></script> in HTML?

  • @Rafaelmofati copy and paste wrong. Now it’s fixed.

  • Thanks @Anmaia, I will take the test tomorrow morning to see the result. I will only be near the equipment in the morning.

  • Place scripts at the end of body is a bit old-fashioned, I would say to use a $(document).ready or something similar, just to make it more understandable.

  • I’m accepting ready-made code @Patrick. My knowledge of web programming is very limited.

  • @Patrick from what I understand, he can’t wear anything other than what’s already there. Since he said the equipment is limited. I guess he can’t use jQuery.

  • jQuery runs on the @Anmaia @Patrick server? If I don’t depend on it I might use.

  • @Rafaelmofati Patrick’s suggestion is valid, but do it the way he suggested you should add the lib jQuery on your html page. As you said it is limited, this is the way to solve your problem with the least possible change.

  • In my case, @Rafaelmofati, yes. jQuery will work. Just add the reference to your html file.

  • I have jQuery yes. I use it to callback if I’m not mistaken. @Anmaia

  • Code suggestion with jQuery? We just have to remember cross-Omain is a big problem for me. And since I don’t have internet on-site, I can’t use proxy services to treat that. @

  • @Rafaelmofati, I suggest you leave it the way it is. If it solves your problem, I don’t think adding jquery lib will improve your solution. I’m a fan of MAIN KISS :). Maybe Patrick can suggest an implementation with jQuery.

  • OK @Anmaia, tomorrow morning I will take the test with the code you passed. Anything else I put something here. Thank you!

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.