2016-05-17 14 views
0

Wie kann ich sicherstellen, dass der Timer jeden Tag auf die Hälfte eingestellt ist, sodass ich kein bestimmtes Datum in Javascript angeben muss? Auch wie ist es möglich, die Ausgabe in einem div oder p statt Form anzuzeigen.Wie kann ich sicherstellen, dass der Timer jeden Tag auf die Hälfte eingestellt ist, sodass ich kein bestimmtes Datum in Javascript angeben muss?

<!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>

+1

Allgemeinen, wenn Sie etwas passieren zu bestimmten Zeiten wollen, sollten Sie Serverzeit dafür verwenden, da der Benutzer die Uhr mühelos ändern können auf sein/ihr Computer. – adeneo

+0

Könnten Sie eine Demo aufstellen, was Sie hier meinen? Vielen Dank – JSEveryday

Antwort

0

<!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"> 
 

 
\t function getTime() { 
 
\t \t now = new Date(); 
 

 
\t \t /* 
 
\t \t \t Use todays date to calculate tomorrows alarm 
 
\t \t */ 
 

 
\t \t var day = now.getDate() 
 
\t \t /*console.log('today: ' + day)*/ 
 

 
\t \t var monthsAbbrev = [ 
 
\t \t \t "Jan", 
 
\t \t \t "Feb", 
 
\t \t \t "Mar", 
 
\t \t \t "Apr", 
 
\t \t \t "May", 
 
\t \t \t "June", 
 
\t \t \t "July", 
 
\t \t \t "Aug", 
 
\t \t \t "Sept", 
 
\t \t \t "Oct", 
 
\t \t \t "Nov", 
 
\t \t \t "Dec" 
 
\t \t ]; 
 
\t \t var index = now.getMonth() 
 
\t \t var month = monthsAbbrev[ index ] 
 
\t \t /*console.log('month: ' + month)*/ 
 

 
\t \t var year = now.getFullYear() 
 
\t \t /*console.log('year: ' + year)*/ 
 

 

 

 

 
\t \t var monthsWith31days = [ 
 
\t \t \t "Jan", 
 
\t \t \t "Mar", 
 
\t \t \t "May", 
 
\t \t \t "July", 
 
\t \t \t "Aug", 
 
\t \t \t "Oct", 
 
\t \t \t "Dec" 
 
\t \t ]; 
 

 
\t \t var monthsWith30days = [ 
 
\t \t \t "Apr", 
 
\t \t \t "June", 
 
\t \t \t "Sept", 
 
\t \t \t "Nov" 
 
\t \t ]; 
 

 

 
\t \t var alarm = "04:30:00 PM" 
 
\t \t var time = now.toLocaleTimeString() /* "HH:MM:ss AM/PM" */ 
 
\t \t /*console.log('time: ' + time) 
 
\t \t console.log('is it true??' + (time >= alarm))*/ 
 
\t \t if (time >= alarm) { 
 
\t \t \t console.log('today is ' + day) 
 

 
\t \t \t var maxIs31 = new RegExp("(" + monthsWith31days.join("|") + ")").test(month) 
 
\t \t \t var maxIs30 = new RegExp("(" + monthsWith30days.join("|") + ")").test(month) 
 
\t \t \t var monthIsFeb = /(Feb)/.test(month) 
 

 
\t \t \t /*console.log('maxIs31: ' + maxIs31) 
 
\t \t \t console.log('maxIs30: ' + maxIs30) 
 
\t \t \t console.log('monthIsFeb: ' + monthIsFeb)*/ 
 

 
\t \t \t if (maxIs31) { 
 
\t \t \t \t (day >= 31) ? (day = 1) : (day +=1) 
 
\t \t \t } else if (maxIs30) { 
 
\t \t \t \t (day >= 30) ? (day = 1) : (day +=1) 
 
\t \t \t } else if (monthIsFeb) { 
 
\t \t \t \t if (parseInt(year, 10) % 4 === 0) { 
 
\t \t \t \t \t /* its a leap year */ 
 
\t \t \t \t \t (day >= 29) ? (day = 1) : (day += 1) 
 
\t \t \t \t } else { 
 
\t \t \t \t \t (day >= 28) ? (day = 1) : (day += 1) 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t \t console.log('tomorrow wil be?? ' + day) 
 
\t \t } 
 

 

 
\t \t /* 
 

 
\t \t \t Now set tomorrows alarm only if its gone off today 
 
\t \t */ 
 
\t \t orderBy = new Date(month +" "+ day +" "+ year +" "+ alarm); 
 

 

 
\t \t days = (orderBy - now)/1000/60/60/24; 
 
\t \t daysRound = Math.floor(days); 
 
\t \t hours = (orderBy - now)/1000/60/60 - (24 * daysRound); 
 
\t \t hoursRound = Math.floor(hours); 
 
\t \t minutes = (orderBy - now)/1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound); 
 
\t \t minutesRound = Math.floor(minutes); 
 
\t \t seconds = (orderBy - now)/1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound); 
 
\t \t secondsRound = Math.round(seconds); 
 
\t \t sec = (secondsRound == 1) ? "" : ""; 
 
\t \t min = (minutesRound == 1) ? " : " : " : "; 
 
\t \t hr = (hoursRound == 1) ? " : " : " : "; 
 
\t \t //dy = (daysRound == 1) ? " day" : " days, "; 
 
\t \t document.timeForm.input1.value = "Check again before " + hoursRound + hr + minutesRound + min + secondsRound + sec; 
 
\t \t newtime = window.setTimeout("getTime();", 1000); 
 
\t } 
 
\t window.onload=getTime; 
 

 
\t if (days = 0){ 
 
\t \t days.style.display = "none"; 
 
\t } 
 
</script> 
 

 
<form name="timeForm"> 
 
\t <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>