使用一个倒计时脚本进行多次倒计时

时间:2012-07-26 08:24:50

标签: javascript forms countdown

我想使用像这样的倒计时脚本

     var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")

function countdown(yr,m,d,idCountdown){
    var theyear=yr;
    var themonth=m;
    var 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;
    var futurestring=montharray[m-1]+" "+d+", "+yr;
    var dd=Date.parse(futurestring)-Date.parse(todaystring);
    var dday=Math.floor(dd/(60*60*1000*24)*1);
    var dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
    var dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
    var dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1);
    var dag = "dagen";

    if(dday==0&&dhour==0&&dmin==0&&dsec==1){
        document.getElementById(idCountdown).value=current;
        return;
    }
    else if(dday<=1) {
        dag = 'dag';
    }
    document.getElementById(idCountdown).value=dday+ ' ' + dag + ' ' +dhour+" uur "+dmin+" min "+dsec+" sec";
    setTimeout(function(){countdown(theyear,themonth,theday,idCountdown);}, 1000);
}

在我的html页面中,我将有一个像这样的文本域

 <input name="aktie1" type="text" class="countdownTekst" size="27" readonly="readonly">
 <input name="aktie2" type="text" class="countdownTekst" size="27" readonly="readonly">

然后我用它来使用函数

countdown(2012,7,30, 'aktie1')

最后一个是文本字段的ID。但是,当我这样做时,我会收到错误

 document.forms.count.idCountdown.value is undefined

如何在该函数中获取该文本字段的ID?

2 个答案:

答案 0 :(得分:1)

您需要访问以下输入:document.forms.count[idCountdown].value

编辑:这个小提琴正在运作http://jsfiddle.net/Fy2FM/4/。更大的问题是您没有使用var yourvar声明变量,这使得变量成为全局对象的一部分。因此,在两次倒计时你总是看到相同的,因为实际上你正在访问相同的变量。

答案 1 :(得分:0)

其中在此脚本中添加特定时间,例如: 3:44

<script language="javascript">countdown(2016,6,20,15:44 'aktie1')</script>