[解説] 時間とともにメッセージを変えるためには、Dateオブジェクトで時刻を読み込んで、ifによる条件に応じてメッセージを設定します。サンプルでは、タイマsetTimeoutによって0.5秒(500msec)毎に繰り返し処理をさせています。 尚、このプログラムはNetscapeでは動きません。(原因調査中) |
Source <html><head> <script language="JavaScript"> <!-- function aisatu() { now = new Date(); var mes; sec = now.getSeconds(); if (sec<10) {document.bgColor="red";mes="おはよう"}; else if (sec<20) {document.bgColor="yellow";mes="こんにちは"}; else if (sec<30) {document.bgColor="blue";mes="さようなら"}; else if (sec<40) {document.bgColor="green";mes="ありがとう"}; else if (sec<50) {document.bgColor="orange";mes="ごめんなさい"}; else if (sec<60) {document.bgColor="purple";mes="じゃあまたね"}; document.Watch1.watch1.value = mes; setTimeout("aisatu()",500); }; //--> </script> </head> <body onLoad="aisatu()"> <p> <center> <font size="5"<<b> 10秒毎に背景の色とメッセージが変わります。 </b> </font> <form name="Watch1"> <input size="13" name="watch1"> </form> </center> </body> </html> |