0

I'll explain my problem ...

I have php code that automatically creates a javascript file every two minutes (through cronjobs) in which there is a multidimensional array.

In my website I have a page that uses this javascript file and also there is a function that every minute deletes this file added in the tag head and adds it again. In this way (I hope) to have an updated version of the js file every minute.

My doubt is:

Is it possible that the javascript file is added to the page when it does not exist or is not yet completed?

What can I do to avoid this?

I hope can you suggest me something and apologize for my english

4
  • This sounds like a weird way of retrieving data. Would it not make more sense to have the page dynamically retrieve the data using AJAX instead of physically creating new files, which could lead to caching issues. Better still, use websockets to push events to the connected clients when the data is updated. Commented Feb 22, 2018 at 11:19
  • you dont need to created file ever time, just use a js file and update the array when you want by Ajax or something. Commented Feb 22, 2018 at 11:19
  • the problem is that with ajax i'm terrible :( can you show me example code or suggest something ? thanks... Commented Feb 22, 2018 at 11:22
  • @RoryMcCrossan Certainly is odd, but not as odd as some might think. It sounds very similiar to JSONP. :) Commented Feb 22, 2018 at 11:25

1 Answer 1

1

You probably very new to coding, here is a simple code for data retrieving using ajax

<div id="demo">

</div>

<script>
setInterval(function() {
  getAndUpdateData()
}, 60 * 1000); // 60 * 1000 milsec

function getDataAndUpdate() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      // render your array and update the DOM   
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "path-to-your-file.php", true);
  xhttp.send();
}
</script>

learn more here : https://www.w3schools.com/js/js_ajax_intro.asp

I also recommend to check jQuery (js library) when you are bit comfortable with js. It provides many function to make your life easy.

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

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.