1

抱歉再次打扰,我还有另一个关于 javascript 的问题

这是 mysql 数据库表中时间计数器的代码:

<script type='text/javascript'>        
        function cronometru(timp_ramas) {
            Ore = Math.floor(timp_ramas / 3600);
            minute = Math.floor((timp_ramas - Ore * 3600) / 60);
            secunde = timp_ramas - minute * 60 - Ore * 3600;
            if (Ore < 10){ Ore = "0"+ Ore; }
            if (minute < 10){ minute = "0" + minute; }
            if (secunde < 10){ secunde = "0" + secunde; }
            if (timp_ramas > 0) {

                timp_ramas--;
                document.getElementById("timp").innerHTML = Ore + ':' + minute + ':' + secunde;
                //document.getElementById("cumpara").innerHTML ="<br><a href='piata.php?cumpara=<?php echo $id_functie; ?>'>Cumpara</a>";

                setTimeout("cronometru("+timp_ramas+")", 1000);

            } else {
                document.getElementById("timp").innerHTML = "[Licitatia s-a terminat]";
            //  document.getElementById("cumpara").innerHTML = "[Nu mai poti cumpara]";

            }
        }
</script>

然后在php中,我在while函数中将此脚本调用为表格形式

while($informatie = mysql_fetch_array($sql))
{   
   $timp_ramas = $informatie['data_limita'] - time();

   //........................................

   echo " <td width='40%' align='justify'>
        Pret : ".$informatie['obiect_pret']." 
        <br />
        Timp ramas : <span id='timp'> "; 

   echo " <script type='text/javascript'> cronometru(".$timp_ramas.") </script></span> ";
   if ($informatie['obiect_cantitate'] > 1) 
       echo "<br>Cantitate disponibila: ".$informatie['obiect_cantitate']." ";
   if ($informatie['data_limita'] > 1) 
       echo "<br><a href='piata.php?cumpara=".$informatie['id']."'>Cumpara</a>";

   echo "</td>";
}

我不明白为什么如果我有 3 行,每行都有来自数据库的不同信息,那么只有首先访问 javascript。其他行完美无缺,它们从数据库中检索所有信息,但这并不希望显示每一行的计时器(倒计时)。为什么?

4

1 回答 1

0

这是我为我的拍卖网站编写的一些代码。它对列出的每个项目都有实时倒计时。我猜你正在做同样的事情。查看代码并随意使用它。无论如何,它应该让你走上正确的道路。

function countdown(x,t,b,i) {
var days = Math.floor(t / 60 / 60 / 24);
var hours = Math.floor([t - (days * 86400)] / 60 / 60 );
var minutes = Math.floor([t - (days * 86400) - (hours * 3600)] / 60 );
var seconds = [t - (days * 86400) - (hours * 3600) - (minutes * 60)];
if ( seconds == 1 ){
secondslabel = " Second"}
else {
secondslabel = " Seconds"}
if ( minutes == 1 ){
minuteslabel = " Minute "}
else {
minuteslabel = " Minutes "}
if ( hours == 1 ){
hourslabel = " Hour "}
else {
hourslabel = " Hours "}
if ( days == 1 ){
dayslabel = " Day "}
else {
dayslabel = " Days "}

if ( days == 0 && hours == 0 && minutes == 0){
document.getElementById(x).value=seconds + secondslabel;
document.getElementById(x).style.backgroundColor = '#E19B9B';
}
else if ( days == 0 && hours == 0){
document.getElementById(x).value=minutes + minuteslabel + seconds + secondslabel;
document.getElementById(x).style.backgroundColor = '#E19B9B';
}
else if ( days == 0){
document.getElementById(x).value=hours + hourslabel + minutes + minuteslabel;
document.getElementById(x).style.backgroundColor = '#9BE1A0';
}
else {
if(document.getElementById(x)){
document.getElementById(x).value=days + dayslabel + hours + hourslabel;
document.getElementById(x).style.backgroundColor = '#9BE1A0';
}
}
if ( t < 0 ){
document.getElementById(x).value='Ended';
document.getElementById(x).style.backgroundColor = 'transparent';
document.getElementById(b).value='Ended';
clearInterval (i);
}
}
于 2013-09-10T11:50:29.957 回答