每秒更新Javascript中的倒计时时间

时间:2016-01-10 02:08:38

标签: javascript php datetime time

我有一些PHP脚本,它回显了事件之前还剩多少时间。时间以看起来像23h 15m 4s

的格式输出

这是我的PHP代码:

    $now = new DateTime();
    $future_date = new DateTime($res['post_time']); # post_time is the time of event
    $interval = $future_date->diff($now);
    $time_until = rtrim(sprintf("%s%s%s",
        $interval->h > 0 ? $interval->h . "h " : "",
        $interval->i > 0 ? $interval->i . "m " : "",
        $interval->s > 0 ? $interval->s . "s " : ""
        )
    ); # Format time to not have '0'
   echo "Time until event " . $time_until;

有没有办法在Javascript中每秒更新一次?我想,回声看起来像

echo "Time until event <span id='countdown'>" . $time_until . "</span>";

JS将使用#countdown做什么?

2 个答案:

答案 0 :(得分:2)

您需要做的是:

  • 将收到的时间从php转换为变量。我会建议和阵列像[小时,分钟,秒]。
  • 使用itertools.combinations
  • 在setInterval函数中,您只需减少秒数,分钟数和小时数,并更新范围#sumdown。
geometry

答案 1 :(得分:0)

您必须使用JS setInterval method