我用php从excel文件中获取数据。在此数据之后,使用ajax从php文件中获取。实际上,我想每5分钟从excel文件中获取一次数据并打印页面。我该怎么办?
数据.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();
}
索引.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>
你需要使用 setInterval() javascript 函数并将其设置为 5 分钟。
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);
});