在javascript和PHP中需要帮助倒计时

时间:2017-05-03 17:52:21

标签: javascript countdown

我在倒计时遇到一个担心,但没有给我看任何东西。页面(index.php = require('countdown.php');)只重新加载,我不明白为什么。

你能帮帮我吗?

countdown.php

<span id="Hour"></span><span id="Mins"></span><span id="Secs"></span>
<script>
function refreshCountdown()
{
  $.ajax({
    url: 'chrono.php',
    type: 'POST',
    dataType: 'JSON'
  }).done(function(resp) {
    if (resp.status) {

if(resp.Hour > 9) {$('#Hour').html("" + resp.Hour + ":");} else {$('#Hour').html("0" + resp.Hour + ":");}
if(resp.Mins > 9) {$('#Mins').html("" + resp.Mins + ":");} else {$('#Mins').html("0" + resp.Mins + ":");}
if(resp.Secs > 9) {$('#Secs').html("" + resp.Secs)      ;} else {$('#Secs').html("0" + resp.Secs)      ;}

    }
    else {
       window.location.reload();
    }
  });
}
$(function() {
    setInterval(refreshCountdown, 1000);
});
</script>

chrono.php

<?php
$countdown = strtotime("10:10");
$diff = $countdown - time();
$ret = new stdClass;
$ret->status = false;

if ($diff > 0) {
  $ret->Hour = (int)($diff / 3600);
  $diff -= $ret->Hour * 3600;
  $ret->Mins = (int)($diff / 60);
  $ret->Secs = ($diff - $ret->Mins * 60);
  $ret->status = true;
}

die(json_encode($ret));
?>

0 个答案:

没有答案