简体   繁体   English

卡住创建php / javascript倒数计时器

[英]Stuck creating a php/javascript countdown timer

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. 最终,我希望从上午8点开始每5个小时重置一次。 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. 我是时代和javascript的新手,所以很难。

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 . 注意, s <= -1在逻辑上等效于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 ? 在下一行中, if(h>= 0 && m < 0 && s <= -1 )在地球上将如何评估为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... :) 快乐编码... :)

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

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