我怎样才能确保计时器设置为每天 4 点半,这样我就不必在 Javascript 中提及某个日期?



我如何确保计时器设置为每天半个4,这样我就不必在Javascript中提到某个日期了?此外,如何可能以div或p而不是形式显示输出。

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>jQuery Countdown</title>
</head>
<body>
<script language="javascript" type="text/javascript">
    function getTime() {
        now = new Date();
        orderBy = new Date("May 17 2016 16:30:00");
        //days = (orderBy - now) / 1000 / 60 / 60 / 24;
        daysRound = Math.floor(days);
        hours = (orderBy - now) / 1000 / 60 / 60 - (24 * daysRound);
        hoursRound = Math.floor(hours);
        minutes = (orderBy - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
        minutesRound = Math.floor(minutes);
        seconds = (orderBy - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
        secondsRound = Math.round(seconds);
        sec = (secondsRound == 1) ? "" : "";
        min = (minutesRound == 1) ? " : " : " : ";
        hr = (hoursRound == 1) ? " : " : " : ";
        //dy = (daysRound == 1)  ? " day" : " days, ";
        document.timeForm.input1.value = "Check again before " + hoursRound + hr + minutesRound + min + secondsRound + sec;
        newtime = window.setTimeout("getTime();", 1000);
    }
    window.onload=getTime;
    if (days = 0){
        days.style.display = "none";
    }
</script>
<form name=timeForm>
    <input type=text name=input1 size=50 border-style="none" style="display: block; border: none; font-size: 14px; font-family: sans-serif">
</form>
</body>
</html>

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>jQuery Countdown</title>
</head>
<body>
<script language="javascript" type="text/javascript">
	function getTime() {
		now = new Date();
		/*
			Use todays date to calculate tomorrows alarm
		*/
		var day = now.getDate()
		/*console.log('today: ' + day)*/
		var monthsAbbrev = [
			"Jan",
			"Feb",
			"Mar",
			"Apr",
			"May",
			"June",
			"July",
			"Aug",
			"Sept",
			"Oct",
			"Nov",
			"Dec"
		];
		var index = now.getMonth()
		var month = monthsAbbrev[ index ]
		/*console.log('month: ' + month)*/
		var year = now.getFullYear()
		/*console.log('year: ' + year)*/

		var monthsWith31days = [
			"Jan",
			"Mar",
			"May",
			"July",
			"Aug",
			"Oct",
			"Dec"
		];
		var monthsWith30days = [
			"Apr",
			"June",
			"Sept",
			"Nov"
		];
		var alarm = "04:30:00 PM"
		var time = now.toLocaleTimeString() /* "HH:MM:ss AM/PM" */
		/*console.log('time: ' + time)
		console.log('is it true??' + (time >= alarm))*/
		if (time >= alarm) {
			console.log('today is ' + day)
			var maxIs31 = new RegExp("(" + monthsWith31days.join("|") + ")").test(month)
			var maxIs30 = new RegExp("(" + monthsWith30days.join("|") + ")").test(month)
			var monthIsFeb = /(Feb)/.test(month)
			/*console.log('maxIs31: ' + maxIs31)
			console.log('maxIs30: ' + maxIs30)
			console.log('monthIsFeb: ' + monthIsFeb)*/
			if (maxIs31) {
				(day >= 31) ? (day = 1) : ( day +=1 )
			} else if (maxIs30) {
				(day >= 30) ? (day = 1) : ( day +=1 )
			} else if (monthIsFeb) {
				if (parseInt(year, 10) % 4 === 0) {
					/* its a leap year */
					(day >= 29) ? (day = 1) : ( day += 1 )
				} else {
					(day >= 28) ? (day = 1) : ( day += 1 )
				}
			}
			console.log('tomorrow wil be?? ' + day)
		}
		/*
			Now set tomorrows alarm only if its gone off today
		 */
		orderBy = new Date( month +" "+ day +" "+ year +" "+ alarm );
		days = (orderBy - now) / 1000 / 60 / 60 / 24;
		daysRound = Math.floor(days);
		hours = (orderBy - now) / 1000 / 60 / 60 - (24 * daysRound);
		hoursRound = Math.floor(hours);
		minutes = (orderBy - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);
		minutesRound = Math.floor(minutes);
		seconds = (orderBy - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
		secondsRound = Math.round(seconds);
		sec = (secondsRound == 1) ? "" : "";
		min = (minutesRound == 1) ? " : " : " : ";
		hr = (hoursRound == 1) ? " : " : " : ";
		//dy = (daysRound == 1)  ? " day" : " days, ";
		document.timeForm.input1.value = "Check again before " + hoursRound + hr + minutesRound + min + secondsRound + sec;
		newtime = window.setTimeout("getTime();", 1000);
	}
	window.onload=getTime;
	if (days = 0){
		days.style.display = "none";
	}
</script>
<form name="timeForm">
	<input type="text" name="input1" size="50" border-style="none" style="display: block; border: none; font-size: 14px; font-family: sans-serif">
</form>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新