前提・実現したいこと
javascriptでカウントダウンを1ページ内に2つ表示させたいです。
発生している問題・エラーメッセージ
カウントダウンが1つしか表示されません。
該当のソースコード
<html lang="ja"> <head> <script type="text/javascript"> // 期限の設定 var gYear = 2019, // 年 gMonth = 2, // 月 gDay = 28, // 日 gHour = 23, // 時 gMinute = 59, // 分 gSecond = 59; // 秒 var goal = new Date(gYear, gMonth-1, gDay, gHour, gMinute, gSecond); // 使用する変数の宣言 var currentTime, period, cDay, cHour, cMinute, cSecond, insert = ""; // カウントダウンの処理1 window.onload = function countdown() { // 現在から期限日までの差を取得 currentTime = new Date(); period = goal.getTime() - currentTime.getTime(); // 期限を過ぎていない時 if(period >= 0) { // 日数を取得 cDay = Math.floor(period / (1000 * 60 * 60 * 24)); period -= (cDay * (1000 * 60 * 60 * 24)); // 時間を取得 cHour = Math.floor(period / (1000 * 60 * 60)); period -= (cHour * (1000 * 60 * 60)); // 分を取得 cMinute = Math.floor(period / (1000 * 60)); period -= (cMinute * (1000 * 60)); // 秒を取得 cSecond = Math.floor(period / (1000)); period -= (cSecond * (1000)); // フレーム cFlame = Math.floor(period / (10)); // 残り日数の書き換え insert = "終了まであと"; insert += '<span class="d">' + cDay + '</span>' + "日"; insert += '<span class="h">' + cHour + '</span>' + "時間"; insert += '<span class="m">' + cMinute + '</span>' + "分"; insert += '<span class="s">' + cSecond + '</span>' + "秒"; document.getElementById('countdown-unit1').innerHTML = insert; // カウントダウンの処理を再実行 setTimeout(countdown, 10); // 期限を過ぎた時 } else { document.getElementById('countdown-unit1').innerHTML = 'Time Up'; } } // 処理の実行 countdown(); </script> </head> <body> <center><font size="4" color="#ff1493"><b><div id="countdown-unit1"></div></b></font></center> </body> </html>
試したこと
// カウントダウンの処理2 window.onload = function countdown2() { // 現在から期限日までの差を取得 currentTime = new Date(); period = goal.getTime() - currentTime.getTime(); // 期限を過ぎていない時 if(period >= 0) { // 日数を取得 cDay = Math.floor(period / (1000 * 60 * 60 * 24)); period -= (cDay * (1000 * 60 * 60 * 24)); // 時間を取得 cHour = Math.floor(period / (1000 * 60 * 60)); period -= (cHour * (1000 * 60 * 60)); // 分を取得 cMinute = Math.floor(period / (1000 * 60)); period -= (cMinute * (1000 * 60)); // 秒を取得 cSecond = Math.floor(period / (1000)); period -= (cSecond * (1000)); // フレーム cFlame = Math.floor(period / (10)); // 残り日数の書き換え insert = "終了まであと"; insert += '<span class="d">' + cDay + '</span>' + "日"; insert += '<span class="h">' + cHour + '</span>' + "時間"; insert += '<span class="m">' + cMinute + '</span>' + "分"; insert += '<span class="s">' + cSecond + '</span>' + "秒"; document.getElementById('countdown-unit2').innerHTML = insert; // カウントダウンの処理を再実行 setTimeout(countdown, 10); // 期限を過ぎた時 } else { document.getElementById('countdown-unit2').innerHTML = 'Time Up'; } } // 処理の実行 countdown();
以上をhead内に追加し
body内には以下を追加したのですが、この2つ目が表示されません。
<center><font size="4" color="#ff1493"><b><div id="countdown-unit2"></div></b></font></center>
補足情報(FW/ツールのバージョンなど)
初心者で、よく分からなくなっています。お教えください。よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー