使用Ajax在PHP中自动更新时间 [英] Automatically update time in PHP using Ajax

查看:114
本文介绍了使用Ajax在PHP中自动更新时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Ajax自动更新PHP时间.下面的命令通过按f5或重新加载按钮刷新页面来工作.

how automatically update the time in PHP using Ajax..the below command work by refreshing the page by hit f5 or reload button..

echo date('s');

或者如果仅通过在php中使用AJAX来更新时间.

or if it is necessary to update time by only using AJAX in php..

推荐答案

因此,基本上,这可以使用php和AJAX来完成.假设您有两个文件index.php和timer.php.您可以通过执行以下操作来获得所需的结果.

So basically this can be done using php and AJAX. Lets say you have two files index.php and timer.php. You can achieve your desired results by doing the following.

在Index.php中

 <?php

echo "<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset='UTF-8'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0'>
        <script src='http://code.jquery.com/jquery.js'></script>
        <script>
         $(document).ready(function(){
             setInterval(_initTimer, 1000);
         });
         function _initTimer(){
             $.ajax({
                 url: 'timer.php',
                 success: function(data) {
                    console.log(data);
                     data = data.split(':');
                     $('#hrs').html(data[0]);
                     $('#mins').html(data[1]);
                     $('#secs').html(data[2]);
                 }
             });
         }
        </script>
    </head>
    <body>
        <span id='hrs'>0</span>:<span id='mins'>0</span>:<span id='secs'>0</span>
    </body>
</html>";

在timer.php中

 <?php echo date('h:i:s A');

这篇关于使用Ajax在PHP中自动更新时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆