2012-04-20 71 views
0

我必须为投标网站创建倒数计时器。当我将它放在while循环之外时,定时器正常工作。但是,id在循环内不起作用。我究竟做错了什么?这里是我的代码:如何为投标网站制作倒数计时器

<script> 
var before="" 
var current="ended" 
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") 

function countdown(yr,m,d,id){ 
theyear=yr;themonth=m;theday=d 
var today=new Date() 
var todayy=today.getYear() 
if (todayy < 1000) 
todayy+=1900 
var todaym=today.getMonth() 
var todayd=today.getDate() 
var todayh=today.getHours() 
var todaymin=today.getMinutes() 
var todaysec=today.getSeconds() 
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec 
futurestring=montharray[m-1]+" "+d+", "+yr 
dd=Date.parse(futurestring)-Date.parse(todaystring) 
dday=Math.floor(dd/(60*60*1000*24)*1) 
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1) 
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1) 
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1) 
if(dday==0&&dhour==0&&dmin==0&&dsec==0){ 
document.getElementById(id).value=current 
return 
} 
else 
document.getElementById(id).value="Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds left "+before 
setTimeout("countdown(theyear,themonth,theday,id)",1000) 
} 
</script> 
<?php 
include('../connection.php'); 
    $rs = mysql_query("select * from datepic") or die(mysql_error()); 

    if(mysql_num_rows($rs)){ 

     while($row = mysql_fetch_array($rs)) 
     { 
      $date = str_replace('-',',',$row['date']); 

      echo '<input type="text" id="'.$row['id'].'" size=80>'; 
      ?> 
      <script> 
       countdown(<?php echo $date.','.$row['id'];?>) 

      </script> 


      <?php   

     } 
    } 

?> 
+0

我建议你看看这有一个关于漂亮代码的起点:) http://codereview.stackexchange.com/questions/10290/countdown-in-javascript – 2012-04-20 09:07:40

回答

1

变化的setTimeout与此:

setTimeout(function(){countdown(theyear,themonth,theday,id);},1000) 

,你可以让你的功能,jQuery函数那样:

jQuery.fn.countdown = function(yr,m,d){ 
    $that = $(this); 
    theyear=yr;themonth=m;theday=d 
    var today=new Date() 
    var todayy=today.getYear() 
    if (todayy < 1000) 
     todayy+=1900 
    var todaym=today.getMonth() 
    var todayd=today.getDate() 
    var todayh=today.getHours() 
    var todaymin=today.getMinutes() 
    var todaysec=today.getSeconds() 
    var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec 
    futurestring=montharray[m-1]+" "+d+", "+yr 
    dd=Date.parse(futurestring)-Date.parse(todaystring) 
    dday=Math.floor(dd/(60*60*1000*24)*1) 
    dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1) 
    dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1) 
    dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1) 
    if(dday==0 && dhour==0 && dmin==0 && dsec==0){ 
     $that.val(current); 
     return 
    } 
    else 
    $that.val("Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds left "+before); 
setTimeout(function(){ $that.countdown(theyear,themonth,theday);},1000) 
} 

<?php 
include('../connection.php'); 
    $rs = mysql_query("select * from datepic") or die(mysql_error()); 

    if(mysql_num_rows($rs)){ 

     while($row = mysql_fetch_array($rs)) 
     { 
      $date = str_replace('-',',',$row['date']); 

      echo '<input type="text" id="'.$row['id'].'" size=80>'; 
      ?> 
      <script> 
       $("?php echo $row['id'] ?>").countdown(<?php echo $date?>); 

      </script> 


      <?php   

     } 
    } 

?> 
+0

thanx好友帮了很多.... :) – 2012-04-20 09:15:18

+0

如果你不介意我有问题: 是这样做的好方法。 或者你可以建议我一些修改或其他方式 谢谢.. – 2012-04-20 09:46:41

+0

我更新的答案,你可以检查它 – Yorgo 2012-04-20 11:00:32