0

Here's the scenario: - there is a set of variables - these variables indicate the statuses of various equipment - the goal is to display an always-updated status chart

So, I have gotten as far as having a parser in Perl that spits out JavaScript var x = 'y'; type code, and now I'm looking for how to have the HTML or other JavaScript automatically check for updates to this "spit out" code, versus just caching it after the first time.

The closest thing I've seen is to use "setInterval" to have it execute a function, so I went ahead and wrapped the "var" statements in a function with a "setInterval" timer. But will this reliably be always up-to-date, or does it cache the whole function, depending on the browser?

EDIT: I'm not currently using any libraries or anything, and would prefer not to - but I will if I have to.

EDIT2: Finally found what I'm looking for. http://www.philnicholas.com/2009/05/11/reloading-your-javascript-without-reloading-your-page/

Just had to modify the last line to get it to work.

7
  • I'd do this with an AJAX call myself to get the data and refresh the page with the fresh results. You can use setTimeout to time the updates (better than setInterval, as there's no conditionality to its execution). Commented Jul 15, 2016 at 22:05
  • Dude, seriously just try Meteor. You will be more than happy Commented Jul 15, 2016 at 22:11
  • Use AJAX and web sockets Commented Jul 15, 2016 at 22:13
  • Create a page that will give you the y in your example in the form {"x": "y"} (this is JSON-formatted). Create a function that uses AJAX to asynchronously request this data from that URL. Set the variable on page load, and then re-request it either with a setInterval() request to the same function, or a call to a function that executes it's own setImeout() (and calls itself when finished). Commented Jul 15, 2016 at 22:23
  • its definitely harder to parse the code out of human text then vice versa NP != P solved! Commented Jul 15, 2016 at 23:30

2 Answers 2

1

You're already close to the solution. Create a function that makes an ajax call to the 'spit out' you have written in perl.

function getSpitOut() {
    $.ajax({
            async: true,
            type: "GET", 
            url: "spit_out.pl", 
            data: "x=8&y=7",
            success: function(msg){
                // UPDATE CHART      
            }        
      });
}

And another function to make the ajax calls at intervals:

$(document).ready(function() {
    setInterval(getSpitOut, 60000);       // Call getSpitOut every 60s
})

The browser won't cache it because you're updating the chart based on the server side perl script.

Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't look like pure JavaScript... is this using a library?
Using jQuery... i hate it myself, but every freaking website uses it today, it has become a 'must have' if you are talking to anybody about JS. People just assume jQuery is present.
Got it. Would the non-jQuery equivalent be much more complex?
The non-jquery equivalent isn't complex. It's something around XMLHttpRequests for the $.ajax function.
0

Apparently I never closed this.

Slightly modified from: "http://www.philnicholas.com/2009/05/11/reloading-your-javascript-without-reloading-your-page/", I have this:

var docHeadObj = document.getElementsByTagName("head")[0];
var dynamicScript = document.createElement("script");
dynamicScript.type = "text/javascript";
dynamicScript.src = scriptName;
docHeadObj.appendChild(dynamicScript);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.