1

時間を使って投稿するフォームを作ろうとしています。私はなんとかカウントタイムを作ることができました。12:00などから投稿する具体的な時間を入力したいです。出来ますか?

<html>
<head>

</head>
<body>
   <span id="remain"></span>
   <form action="3.php" method="post" id="form1" name="form1">
     <input type="text" name="id">
     <input type="submit" name="Go" value="submit">
   </form>

<script type="text/javascript">
  window.onload=counter;
  function counter() {
     seconds = 150;
     countDown();
  } 

  function countDown(){
     document.getElementById("remain").innerHTML=seconds;
     setTimeout("countDown()",1000);
     if(seconds == 0) {
        document.form1.submit();
     }else {
        seconds--;      
     }
   }    
</script>

4

3 に答える 3

2

これは、現在の時間を見つけて、フォームを投稿する設定時間と現在の時間を比較することで行われます

現在の時刻を検索

var currentTime = new Date ( );
var currentHours = currentTime.getHours ( );
var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
currentHours = ( currentHours == 0 ) ? 12 : currentHours;

上記のコードでは、現在の時刻を見つけて、特定の時刻と比較します。両方の時間が等しい場合は、関数を実行します。

于 2012-11-15T08:23:59.123 に答える
1

関数 countDown をこれに変更します。最初に現在の時刻を取得し、残りの秒を現在の時刻まで計算し、これを秒単位のタイムアウト時間として設定します

function countDown(){

 //current time in seconds
 var d = new Date();
 var n = d.getTime();

 //find the remaining seconds from a day
 var remainingSec = n % (60 * 60 * 24);

 //find current hours set and convert to seconds
 var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
 currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
 currentHours = ( currentHours == 0 ) ? 12 : currentHours;
 var currentSec = currentHours * 60 * 60;

 //set timeout time
 var timeoutTime = remainingSec - currentSec;

 if(timeoutTime > 0) {
  setTimeout(function(){
   document.form1.submit();
  },timeoutTime * 1000);
 } else {
  console.log("Sorry the time is back in the past.");
 }

}
于 2012-11-15T08:32:13.783 に答える
1

このコードを使用できると思います。これはまさにあなたが尋ねた方法ではありませんが、これはアイデアです

    <html>
<head>

</head>
<body>
<span id="remain"></span>
<form action="test1.asp" method="post" id="form1" name="form1">
<input type="text" name="id">
<input type="submit" name="Go" value="submit">
</form>
<script type="text/javascript">
window.onload=counter;
function counter()
{
    var currentTime = new Date ( );

  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );
hours=currentHours
minutes=currentMinutes
seconds = currentSeconds;

countDown();
}   

function countDown(){


document.getElementById("remain").innerHTML=hours+':'+minutes+':'+seconds;
setTimeout("countDown()",1000);
    if(hours == 12)
        {
            document.form1.submit();
        }else {
        seconds--;      
        }
}

</script>
</body>
</html>
于 2012-11-15T08:30:08.023 に答える