Javascript倒计时仅适用于Chrome

时间:2013-12-30 22:07:27

标签: javascript google-chrome countdown

好吧,我正在制作游戏,在我的游戏中,我在javascript中有一个倒计时脚本,它接收建筑物升级结束的日期,并进行倒计时,当倒计时结束时,执行升级的脚本建筑物更上一层楼。

但只适用于谷歌浏览器,在其他浏览器中显示如下:


火狐: Firefox

Google Chrome Google Chrome


只是Javascript:

date_default_timezone_set('europe/lisbon');
$datephp = date('Y-m-d H:i:s');


echo'

<script type="text/javascript">
function cdtd() {
    var xmas = new Date("' . $factory_date . '");
    var now = new Date();
    var timeDiff = xmas.getTime() - now.getTime();
    if (timeDiff <= 0) {
    clearTimeout(timer);
    $("#factory_upgrade").load("/include/factory_upgraded.php");
    $("#quantidade_fabricas").load("/include/factory_stats.php");
    }

    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %= 60;' .
    "var tempo=('0'  + hours).slice(-2)+':'+('0'  + minutes).slice(-2)+':'+('0' + seconds).slice(-2);"
    .
    '
    document.getElementById("secsBox").innerHTML = tempo;
    var timer = setTimeout("cdtd()",1000);
}
</script>
';

完成功能:

function factory_update($get){

$userid = $_SESSION['userid'];

$query00 = "SELECT * FROM factory_upgrading WHERE userid = '$userid'";
$result00 = mysql_query($query00) or die(mysql_error());

while($row00 = mysql_fetch_array($result00)){
$factory_upgrade = $row00['userid'];
}

if(!isset($factory_upgrade)){
echo "Sem melhoramentos.";
return 0;
}

$query01 = "SELECT * FROM factory_upgrading WHERE userid = '$userid'";
$result01 = mysql_query($query01) or die(mysql_error());

while($row01 = mysql_fetch_array($result01)){
$factory_level = $row01['new_level'];
$factory_date = $row01['upgraded'];
}


if ($get == "load")
{
echo '<div class="message_upgrades" ">';
echo '<div class="loading"><img src="/images/loading.gif"></img></div>';
echo '</div>';}

else
{

date_default_timezone_set('europe/lisbon');
$datephp = date('Y-m-d H:i:s');


echo'

<script type="text/javascript">
function cdtd() {
    var xmas = new Date("' . $factory_date . '");
    var now = new Date();
    var timeDiff = xmas.getTime() - now.getTime();
    if (timeDiff <= 0) {
    clearTimeout(timer);
    $("#factory_upgrade").load("/include/factory_upgraded.php");
    $("#quantidade_fabricas").load("/include/factory_stats.php");
    }

    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %= 60;' .
    "var tempo=('0'  + hours).slice(-2)+':'+('0'  + minutes).slice(-2)+':'+('0' + seconds).slice(-2);"
    .
    '
    document.getElementById("secsBox").innerHTML = tempo;
    var timer = setTimeout("cdtd()",1000);
}
</script>
';

echo '<div class="success_upgrades">';
echo '<div class="upgrade_text"><b>Nivel: </b>' . $factory_level . '</div><div class="div_separator"></div><div class="upgrade_text"><b>Duração:</b>
 <div class="secsBox" id="secsBox"></div>
<script type="text/javascript">cdtd();</script></div><div class="div_separator"></div>
';
echo '<div id="close" class="stop_upgrade" ><a href="#" id="close" ><img src="/icon/x.png"></img></a></div>';
echo '</div>';

echo '<script>


    $(".stop_upgrade").click(function (e) {
    e.preventDefault();
    $(factory_upgrade2).empty();



    setTimeout(function(){
    $("#factory_upgrade2").load("/include/upgrade_cancel.php");
    $("#factory_upgrade").load("/include/factory_update.php");
    $("#load").load("/include/cabecalho_content.php");
    $("#industrial").fadeIn();
                $(loader1).delay(1000).hide(0);
                }, 100);


    });


    </script>
    ';
}
}    

1 个答案:

答案 0 :(得分:1)

根据标准JavaScript,$factory_date的格式无效,而Chrome恰好可以解析它。

为了获得更好的结果,请坚持使用这些Date构造函数:

new Date();
new Date(value);
new Date(dateString);
new Date(year, month [, day, hour, minute, second, millisecond]);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date