duanmengsuo9302 2013-07-26 13:26
浏览 42

使用YII中的时间间隔调用渲染部分

I want to update a div contents automatically with refresh whole page. So i did Ajax renderPartial in YII. Now I implement using AJAX button onclick

My code as follows

<?php 
      echo CHtml::ajaxButton ("Update data",
      CController::createUrl("blog/UpdateAjax?url=$url"), 
      array('update' => '#inrscrn'));
?>

Now I want to render with in a time limit please help

  • 写回答

1条回答 默认 最新

  • dougui5419 2013-07-29 09:22
    关注

    Your question is not very clear. I suppose you want to setup an automatical & periodical refresh of the content within a div instead of clicking on the button.

    This is the JavaScript you need on your page:

    <script type="text/javascript">
        timeout = 60 * 1000; // in Milliseconds -> multiply with 1000 to use seconds
        function refresh() {
            <?php
            echo CHtml::ajax(array(
                    'url'=> CController::createUrl("blog/UpdateAjax?url=".$url),
                    'type'=>'post',
                    'update'=> '#inrscrn',
            ))
            ?>
        }
        window.setInterval("refresh()", timeout);
    </script>
    

    But it is not a good approach to send an URL to your controler, rather make a direct request to to make a special AJAX return of a controler which needs to return the correspondent data.

    <?php
    public function actionTest(){
            if (isset($_REQUEST['AJAX']) || Yii::app()->getRequest()->getIsAjaxRequest()) {
                $this->renderPartial(
                    'test',
                    array('model' => $model),
                    false,
                    true
                );
            } else {
                $this->render(
                    'test',
                    array('model' => $model),
                );
            }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制