title: Get PHP variable every minute
body: <p>I'm using flot realtime charts and I'm having trouble refreshing my values on the chart.
  I have a PHP code to fetch a value and store it to a variable but as I came up to learn the PHP code rus before the pnage is displayed on the browser and it only runs again if I refresh the page. 
  Is there a way to make that PHP code fetch the value from the file every minute and plot it?</p>
  
  <p>This is my code:</p>
  
  <pre><code>&lt;?php
      function act(){
      $filename = "(...)/CS.txt";
      $handle = fopen($filename, "r");
      $contents = fread($handle, filesize($filename));
      fclose($handle);
      return $contents;
      };
  ?&gt;
  &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://static.pureexample.com/js/flot/jquery.flot.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://static.pureexample.com/js/flot/jquery.flot.time.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://static.pureexample.com/js/flot/jquery.flot.axislabels.js"&gt;&lt;/script&gt;
  
  &lt;!-- CSS --&gt;
  &lt;style type="text/css"&gt;
      #flotcontainer {
          width: 95%;
          height: 95%;
          text-align: center;
          margin: 0 auto;
      }
  &lt;/style&gt;
  
  &lt;!-- Javascript --&gt;
  &lt;script&gt;
  function cs() {
          var val = &lt;?php echo act(); ?&gt;;
          return val;
  };
  
      var data = [];
      var dataset;
      var totalPoints = 50;
      var updateInterval = 1000;
      var now = new Date().getTime();
  
  
      function GetData() {
          data.shift();
  
          while (data.length &lt; totalPoints) {
              var y = cs();
              var temp = [now += updateInterval, y];
  
              data.push(temp);
          }
      }
  
      var options = {
          series: {
              lines: {
                  show: true,
                  lineWidth: 1.2,
                  fill: true
              }
          },
          xaxis: {
              mode: "time",
              tickSize: [2, "second"],
              tickFormatter: function (v, axis) {
                  var date = new Date(v);
  
                  if (date.getSeconds() % 20 == 0) {
                      var hours = date.getHours() &lt; 10 ? "0" + date.getHours() : date.getHours();
                      var minutes = date.getMinutes() &lt; 10 ? "0" + date.getMinutes() : date.getMinutes();
                      var seconds = date.getSeconds() &lt; 10 ? "0" + date.getSeconds() : date.getSeconds();
  
                      return hours + ":" + minutes + ":" + seconds;
                  } else {
                      return "";
                  }
              },
              axisLabel: "Horas",
              axisLabelUseCanvas: true,
              axisLabelFontSizePixels: 12,
              axisLabelFontFamily: 'Verdana, Arial',
              axisLabelPadding: 10
          },
          yaxis: {
              min: 0,
              max: 300,
              tickSize: 5,
              tickFormatter: function (v, axis) {
                  if (v % 10 == 0) {
                      return v + "";
                  } else {
                      return "";
                  }
              },
              axisLabel: "Número de CS's",
              axisLabelUseCanvas: true,
              axisLabelFontSizePixels: 12,
              axisLabelFontFamily: 'Verdana, Arial',
              axisLabelPadding: 6
          },
          legend: {
              labelBoxBorderColor: "#fff"
          }
      };
  
      $(document).ready(function () {
          GetData();
  
          dataset = [
               { label: "CS's", data: data }
           ];
  
          $.plot($("#flotcontainer"), dataset, options);
  
          function update() {
              GetData();
  
              $.plot($("#flotcontainer"), dataset, options)
              setTimeout(update, updateInterval);
          }
  
          update();
      });
  
  &lt;/script&gt;
  
  &lt;!-- HTML --&gt;
  &lt;div id="flotcontainer"&gt;&lt;/div&gt;
  </code></pre>