如何为竞价网站制作倒数计时器

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

 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) }  <?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 ''; ?>  countdown()   

用这个改变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) } '; ?>