首页 > 程序开发 > web前端 > JavaScript >

phppjaxJS自动刷新

2017-02-20

phppjaxJS自动刷新

time.php

<?php
use yii\widgets\Pjax;
use yii\helpers\Html;
 
Pjax::begin([&#39;id&#39; => &#39;comment&#39;]);
 
$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+&#39;-&#39;+month+&#39;-&#39;+day+&#39; &#39;+hour+&#39;:&#39;+minute+&#39;:&#39;+second;
        console.log(t);
        $(&#39;#timea&#39;).html(t);
        */
 
      console.log("$time");
      $(&#39;#timea&#39;).html("$time");
  }
});
JS;
$this->registerJs($script);
echo Html::a(&#39;time&#39;, [&#39;comment/time&#39;], [&#39;class&#39; => &#39;btn btn-lg btn-primary&#39;, &#39;id&#39; => &#39;time&#39;]);
echo &#39;<hr>&#39;;
echo "<h3>Current Time: {$time}</h3>";
 
Pjax::end();
?>
 
 
?>

Controller.php

public function actionTime()
{
    //设置时区
    date_default_timezone_set(&#39;PRC&#39;);
    return $this->render(&#39;time&#39;,[&#39;time&#39;=>date("Y-m-d H:i:s")]);
}

main.php 布局

&#39;<li ><span id="timea">&#39;.date("h:i:s").&#39;</span></li>&#39;,

相关文章
最新文章
热点推荐