简体   繁体   中英

use one countdown script for multiple countdowns

I want to use a countdown script like this

     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);
}

in my html page i will have a textfield like this

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

Then i use this to use the function

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

The last one is the ID for the textfield. But when i do it like this i will get an error

 document.forms.count.idCountdown.value is undefined

How do i get the id for that textfield in that function?

You need to access the input like this: document.forms.count[idCountdown].value

EDIT: This fiddle is working http://jsfiddle.net/Fy2FM/4/ . The bigger problem is that you are not declaring the variables with var yourvar , and this makes the variables to be part of the global object. Because of this, you were always seeing the same in both countdowns, because in fact you were accessing the same variables.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM