<!DOCTYPE html> <html lang="zh-TW"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>股市倒數計時</title> <style> body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #000; /* 黑色背景 */ color: #fff; /* 白色文字 */ } #countdown { font-size: 2em; color: #0f0; /* 綠色文字 */ } pre { background-color: #333; /* 深灰色背景 */ color: #fff; /* 白色文字 */ padding: 10px; border-radius: 5px; overflow-x: auto; } .keyword { color: #00f; /* 藍色 */ } .string { color: #a22; /* 棕紅色 */ } .comment { color: #777; /* 灰色 */ } </style> </head> <body> <div> <div id="countdown">載入中...</div> <pre> <code> <span class="keyword">function</span> getTimeRemaining(endtime) { <span class="keyword">const</span> total = Date.parse(endtime) - Date.now(); <span class="keyword">const</span> seconds = Math.floor((total / 1000) % 60); <span class="keyword">const</span> minutes = Math.floor((total / 1000 / 60) % 60); <span class="keyword">const</span> hours = Math.floor((total / 1000 / 60 / 60) % 24); <span class="keyword">const</span> days = Math.floor(total / (1000 * 60 * 60 * 24)); <span class="keyword">return</span> { total, days, hours, minutes, seconds }; }
<span class="keyword">function</span> initializeClock(id, startHour, startMinute, endHour, endMinute) { <span class="keyword">const</span> clock = document.getElementById(id);
<span class="keyword">function</span> updateClock() { <span class="keyword">const</span> now = <span class="keyword">new</span> Date(); <span class="keyword">const</span> currentHour = now.getHours(); <span class="keyword">const</span> currentMinute = now.getMinutes(); <span class="keyword">let</span> endtime;
<span class="comment">// 在開盤前</span> <span class="keyword">if</span> (currentHour < startHour || (currentHour === startHour && currentMinute < startMinute)) { endtime = <span class="keyword">new</span> Date(); endtime.setHours(startHour, startMinute, 0, 0); clock.innerHTML = <span class="string">'距離股市開盤還有:'</span> + formatTime(getTimeRemaining(endtime)); } <span class="keyword">else if</span> ((currentHour > startHour || (currentHour === startHour && currentMinute >= startMinute)) && (currentHour < endHour || (currentHour === endHour && currentMinute < endMinute))) { <span class="comment">// 股市開盤中</span> endtime = <span class="keyword">new</span> Date(); endtime.setHours(endHour, endMinute, 0, 0); clock.innerHTML = <span class="string">'距離股市收盤還有:'</span> + formatTime(getTimeRemaining(endtime)); } <span class="keyword">else</span> { <span class="comment">// 股市已收盤,倒數到下一個交易日的開盤</span> endtime = <span class="keyword">new</span> Date(); endtime.setDate(endtime.getDate() + 1); endtime.setHours(startHour, startMinute, 0, 0); clock.innerHTML = <span class="string">'距離股市開盤還有:'</span> + formatTime(getTimeRemaining(endtime)); }
<span class="keyword">if</span> (getTimeRemaining(endtime).total <= 0) { clearInterval(timeinterval); initializeClock(id, startHour, startMinute, endHour, endMinute); } }
<span class="keyword">function</span> formatTime(t) { <span class="keyword">return</span> `${t.hours}小時 ${t.minutes}分鐘 ${t.seconds}秒`; }
updateClock(); <span class="keyword">const</span> timeinterval = setInterval(updateClock, 1000); }
<span class="comment">// 初始化倒數計時,設定股市時間(例如,上午9:00 至下午1:30)</span> initializeClock(<span class="string">'countdown'</span>, 9, 0, 13, 30); </code> </pre> </div>
<script> function getTimeRemaining(endtime) { const total = Date.parse(endtime) - Date.now(); const seconds = Math.floor((total / 1000) % 60); const minutes = Math.floor((total / 1000 / 60) % 60); const hours = Math.floor((total / 1000 / 60 / 60) % 24); const days = Math.floor(total / (1000 * 60 * 60 * 24)); return { total, days, hours, minutes, seconds }; }
function initializeClock(id, startHour, startMinute, endHour, endMinute) { const clock = document.getElementById(id);
function updateClock() { const now = new Date(); const currentHour = now.getHours(); const currentMinute = now.getMinutes(); let endtime;
// 在開盤前 if (currentHour < startHour || (currentHour === startHour && currentMinute < startMinute)) { endtime = new Date(); endtime.setHours(startHour, startMinute, 0, 0); clock.innerHTML = '距離股市開盤還有:' + formatTime(getTimeRemaining(endtime)); } else if ((currentHour > startHour || (currentHour === startHour && currentMinute >= startMinute)) && (currentHour < endHour || (currentHour === endHour && currentMinute < endMinute))) { // 股市開盤中 endtime = new Date(); endtime.setHours(endHour, endMinute, 0, 0); clock.innerHTML = '距離股市收盤還有:' + formatTime(getTimeRemaining(endtime)); } else { // 股市已收盤,倒數到下一個交易日的開盤 endtime = new Date(); endtime.setDate(endtime.getDate() + 1); endtime.setHours(startHour, startMinute, 0, 0); clock.innerHTML = '距離股市開盤還有:' + formatTime(getTimeRemaining(endtime)); }
if (getTimeRemaining(endtime).total <= 0) { clearInterval(timeinterval); initializeClock(id, startHour, startMinute, endHour, endMinute); } }
function formatTime(t) { return `${t.hours}小時 ${t.minutes}分鐘 ${t.seconds}秒`; }
updateClock(); const timeinterval = setInterval(updateClock, 1000); }
// 初始化倒數計時,設定股市時間(例如,上午9:00 至下午1:30) initializeClock('countdown', 9, 0, 13, 30); </script> </body> </html> |