【问题标题】:i want to get data from php file with ajax once per 5 min我想每 5 分钟用 ajax 从 php 文件中获取一次数据
【发布时间】:2016-02-07 15:38:13
【问题描述】:

我正在使用 php 从 excel 文件中获取数据。在使用 ajax 从 php 文件中获取此数据后。事实上,我想每 5 分钟从 excel 文件中获取一次数据并打印页面。我该怎么办?

data.php

include "Classes/PHPExcel/IOFactory.php";


try {

     $url="https://docs.google.com/spreadsheets/d/1ngOuUvGk07r69HEonmYdjl9En1F1COAB8fAhNXNT1Y8/pub";//Bu url 'i load'ın içine girdiğimde File not exist hatası veriyor.Ben localde denemek için aşağıdak inputfile .
     $inputFile = 'a.xlsx';
     $objPhpExcel = PHPExcel_IOFactory::load($inputFile);
     $rows = $objPhpExcel->getActiveSheet()->toArray(null, true, true, true);

     $i=0;
     $data_en=array();
     $data_tr=array();
     $word=array();
        foreach ($rows as $row)
        {
            $i++;
            $data_en[$i] = $row['C'];
            $data_tr[$i]= $row['D'];
            echo $data_en[$i];echo "<br>";
        }


    }
catch(PHPExcel_Exception $e)
{
echo $e->getMessage();
} 

index.html

   
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
    $(function(){
        $.get("data.php", function(data){
            $('#container').html(data);
        });
    });
</script>
<body>
<p id="container"></p>

</body>

【问题讨论】:

    标签: php ajax phpexcel phpexcelreader


    【解决方案1】:

    您需要使用 setInterval() javascript 函数并将其设置为 5 分钟。

    【讨论】:

      【解决方案2】:

      JavaScript setInterval() 函数可用于自动执行任务 使用基于常规时间的触发器。 你也可以通过clearInterval()清除预定的工作

      它是一个原生 JavaScript 函数。

      var duplicateWork = setInterval(function() {
            // Do something every 1 seconds
      }, 1000);
      
      // To cancel scheduled work use similar code 
      
      clearInterval(duplicateWork);
      

      看例子:

      var l = $('#list');
      
      var duplicateWork = null;
      var seconds = 1000 * 2;//2 second
      
      $('#start').click(function(){
        $('#title').html('Start : add item once per 2 second');
        duplicateWork = setInterval(function() {
                // Do something every 2 seconds
            l.append('<li>duplicate work</li>');
          }, seconds);
      });
      
      $('#stop').click(function(){
        $('#title').html('Stop');
        clearInterval(duplicateWork);
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <button id="start">Start</button>
      <button id="stop">Stop</button>
      <h3 id="title"></h3>
      <hr />
      <ul id="list">
      
      </ul>

      在你的情况下,你可以使用类似的代码

      $(document).ready(function()
      {
          var seconds = 1000 * (60 * 5);//5 minute
          var refreshId = setInterval( function() {
              $.get("data.php", function(data){
                  $('#container').html(data);
              });
          }, seconds);
      });
      

      【讨论】:

        猜你喜欢
        • 2013-03-14
        • 1970-01-01
        • 2014-03-28
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 2013-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多