duandi8613 2013-12-07 18:21 采纳率: 100%
浏览 57

坚持创建一个php / javascript倒计时器

I have been struggling to get my head around this for a while now.

I am attempting to create a countdown timer. Eventually I want it to reset after every 5 hours starting from 8am. But for now I can't figure out if im setting the hours, minutes and seconds correctly to count down together properly.

This is my code so far:

<?php
    $timeTo  = strtotime('08:00:00').'<br />';
                $timeNow = strtotime('now').'<br />';
                $differenceInSeconds = $timeTo - $timeNow;
            ?>
            <script type="text/javascript">
                var s= "<?php Print($differenceInSeconds);?>";
                var h= Math.floor(s/3600);
                s-= h*3600;
                var m= Math.floor(s/60);
                s -= m*60;
                var counter=setInterval(timer, 1000); //1000 will  run it every 1 second
                    function timer()
                    {

                      s=s-1;
                      if(h >= 0 && m >= 0 && s <= -1){
                        m=m-1;
                        s=59;
                        if(h>= 0 && m < 0 && s <= -1){
                            h=h-1;
                            m=59;
                            s=59;
                              if (s <= -1)
                              {

                                 //counter ended, reset counter


                                 return;
                              }
                          }
                        }
                        //Do code for showing the number of seconds here
                          document.getElementById("timer").innerHTML=(h <= 0 ? ' ' : h+"hr ")+(m <= 0 ? ' ' : m+"min ")+(s < 10 ? '0'+s : s+"secs "); // watch for spelling
                    }
            </script>

Am I barking up the wrong tree here? I am new to times and javascript so finding it difficult.

  • 写回答

1条回答 默认 最新

  • doumu5662 2013-12-07 18:44
    关注

    I will not answer this. I will just try to lead you to the answer.

    1. Note that s <= -1 is logically equivalent to s < 0. There is no confusion about it. Use the second one, it looks more clean.
    2. s=59; and in the next line how on earth will if(h>= 0 && m < 0 && s <= -1 ) ever evaluate to true ?

    Similar other logical mistakes are present as well. Take some time and fix this. If you fix this on your own then you're one step closer to becoming a good programmer.

    Happy Coding... :)

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器