php pjax JS自动刷新
time.php
<?php
use yii\widgets\Pjax;
use yii\helpers\Html;
Pjax::begin(['id' => 'comment']);
$script = <<< JS
$(document).ready(function() {
var ts=setInterval(clock, 1000);
function clock()
{
clearInterval(ts);//清除时间记数
//Pjax刷新
$.pjax.reload({container:"#comment"});
/* JS获取数据
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
var t=year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
console.log(t);
$('#timea').html(t);
*/
console.log("$time");
$('#timea').html("$time");
}
});
JS;
$this->registerJs($script);
echo Html::a('time', ['comment/time'], ['class' => 'btn btn-lg btn-primary', 'id' => 'time']);
echo '<hr>';
echo "<h3>Current Time: {$time}</h3>";
Pjax::end();
?>
?>
Controller.php
public function actionTime()
{
//设置时区
date_default_timezone_set('PRC');
return $this->render('time',['time'=>date("Y-m-d H:i:s")]);
}
main.php 布局
'<li ><span id="timea" style="color: white">'.date("h:i:s").'</span></li>',