繁体   English   中英

如何在PHP中使用jQuery else自动刷新特定的div

[英]How to auto refresh a specific div using jQuery else in PHP

希望您周末愉快。

我在php文件中添加了一些简单的代码,其中显示了到指定日期和时间为止的剩余时间。

例如:

<div class="time-remained">
// php code for displaying time remaining
</div>

我希望时间每秒钟自动刷新一次。 为此,是否可以使用jQuery或其他方法每秒自动刷新特定的div?

非常感谢你。

您可以使用CoffeeScript和Moment.js动态增加时钟。 看一下这个链接。 Coffeescript:使用coffeescript动态更新js时刻

我认为我有理想,

编写一个JavaScript函数以向您的php时间脚本发出请求,并使用setInterval()每秒刷新一次div。

仅作为示例。

<script>
  function chk_ajax_with_php(){
    var params = date.now();

       var url = "URL_TO_PHP_SCRIPT";
            $.ajax({
                           type: 'POST',
                           url: url,
                           dataType: 'html',
                           data: params,
                           beforeSend: function() {
                          document.getElementById("time-remained").innerHTML= "CHECKING TIME";
                                     },
                           complete: function() {

                           },
                           success: function(html) {


                                 /// html represent your outcome
                                 if(html=='keep'){

                                    chk_ajax_with_php();

                                 }else{

                                      document.getElementById("time-remained").innerHTML= html;
                                 }

                           }
                   });
}

setTimeout(function(){chk_ajax_with_php()}, 1000);


</script>

<div class="time-remained">
// THIS DIV WILL REFRESH EVERY PER SECOND
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM