如何使javascript变量动态为计时器

时间:2015-11-16 01:50:56

标签: javascript timer

好吧,我有一个倒计时的计时器脚本,并以这种格式提供日期:

var year="2015";    

var month="11";      

var day="18"; 

我正在尝试弄清楚如何动态填充上述变量,以便计时器在每天晚上8点重置。因此,在2015年11月18日,这些变量将动态变为:

var year="2015";

var month="11";      

var day="25";

继续前进或者让用户在表单中输入日期以使用正确的信息填充这些变量会更好。如果可能的话,我宁愿这一切都是自动的。

使用变量的脚本:

/*
Count down until any date script-
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free scripts here!
Modified by Robert M. Kuhnhenn, D.O. 
on 5/30/2006 to count down to a specific date AND time,
on 10/20/2007 to a new format, and 1/10/2010 to include
time zone offset.
*/

var current="Citadel has reset!";    //-->enter what you want the script to display when the target date and time are reached, limit to 20 characters

var year="2015";    //-->Enter the count down target date YEAR

var month="11";      //-->Enter the count down target date MONTH

var day="18";       //-->Enter the count down target date DAY

var hour="22";      //-->Enter the count down target date HOUR (24 hour clock)

var minute="00";    //-->Enter the count down target date MINUTE

var tz=-5;        //-->Offset for your timezone in hours from UTC (see http://wwp.greenwichmeantime.com/index.htm to find the timezone offset for your location)

//    DO NOT CHANGE THE CODE BELOW!
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

function countdown(yr,m,d,hr,min){

	theyear=yr;themonth=m;theday=d;thehour=hr;theminute=min;

	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 todaystring1=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec;

	var todaystring=Date.parse(todaystring1)+(tz*1000*60*60);

	var futurestring1=(montharray[m-1]+" "+d+", "+yr+" "+hr+":"+min);

	var futurestring=Date.parse(futurestring1)-(today.getTimezoneOffset()*(1000*60));

	var dd=futurestring-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);

	if(dday<=0&&dhour<=0&&dmin<=0&&dsec<=0){

		document.getElementById('count2').innerHTML=current;

		document.getElementById('count2').style.display="inline";

		document.getElementById('count2').style.width="390px";

		document.getElementById('dday').style.display="none";

		document.getElementById('dhour').style.display="none";

		document.getElementById('dmin').style.display="none";

		document.getElementById('dsec').style.display="none";

		document.getElementById('days').style.display="none";

		document.getElementById('hours').style.display="none";

		document.getElementById('minutes').style.display="none";

		document.getElementById('seconds').style.display="none";

		document.getElementById('spacer1').style.display="none";

		document.getElementById('spacer2').style.display="none";

		return;

	}else {

		document.getElementById('count2').style.display="none";

		document.getElementById('dday').innerHTML=dday;

		document.getElementById('dhour').innerHTML=dhour;

		document.getElementById('dmin').innerHTML=dmin;

		document.getElementById('dsec').innerHTML=dsec;

		setTimeout("countdown(theyear,themonth,theday,thehour,theminute)",1000);

	}

}

1 个答案:

答案 0 :(得分:1)

您可以使用JavaScript的<LowestNewPrice> <Amount>247980</Amount> <CurrencyCode>GBP</CurrencyCode> <FormattedPrice>£2,479.80</FormattedPrice> </LowestNewPrice> 对象。

当您的计时器被触发时,您可以执行以下操作:

Date

您也可以首先使用var date = new Date(year, month, day); date.setDate(date.getDate() + 7); year = date.getFullYear().toString(); month = date.getMonth().toString(); day = date.getDate().toString(); //Note: not date.getDay() even though that seems more correct. 对象而不是三个单独的字符串变量,以避免在不同类型之间创建/解析/转换。