刷新javascript倒计时的麻烦

时间:2012-09-15 13:54:31

标签: php javascript

我倒计时site无法刷新自己。

代码:http://pastebin.com/DNjb0WSN

window.onload = function() {
    idElement = "time";
    document.getElementById(idElement).innerHTML = 
        "<?php countdown(21,00,00,9,15,2012) ?>";

    setInterval("document.getElementById(idElement).innerHTML = 
        '<?php countdown(21,00,00,9,15,2012) ?>'", 1000);
};

// ...

<?php
    function countdown($godzina, $minuta, $sekunda, $miesiac, $dzien, $rok) {
        $target = mktime($godzina, $minuta, $sekunda, $miesiac, $dzien, $rok);
        $now = time();
        $sekundy = ($target - $now);
        // $sekundy =(int) ($sekundy) ;

        $check = 0;
        if ($sekundy > 0) {
            if ($check == 0) {
                if ($sekundy <= 10000) {
                    // zmien klase
                    $check = 1;
                }
            }
            $hours = floor($sekundy / 3600);
            $minutes = floor(($sekundy / 60) - ($hours * 60));
            $seconds = floor(($sekundy) - ($hours * 3600) - ($minutes * 60));


            if ($hours < 10) $hours = '0'.$hours;

            if ($minutes < 10) $minutes = '0'.$minutes;

            if ($seconds < 10) $seconds = '0'.$seconds;

            $all = $hours." : ".$minutes." : ".$seconds;
            echo $all;
        }
        else {
            echo "Aukcja zakończona!";
        }
    }
?>


为什么不起作用?

2 个答案:

答案 0 :(得分:2)

因为倒计时没有在javascript中实现,而是php。如果您需要倒计时在客户端上工作,您需要在javascript中实现它或者轮询服务器( NOT RECOMMENDED )。

网络是无国籍的,是否会响铃?

答案 1 :(得分:0)

尝试使用JavaScript编写的倒计时库并从PHP进行参数化: http://www.hashemian.com/tools/javascript-countdown.htm