javascript 使用时间发布表单

3pmvbmvn  于 12个月前  发布在  Java
关注(0)|答案(3)|浏览(93)

我正在尝试制作一个使用时间发布的表格。我已经设法计算时间。我想输入一个特定的时间来发布从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>

字符串

slmsl1lt

slmsl1lt1#

这是通过查找当前时间,然后将当前时间与您要发布的表单上的设置时间进行比较

查找当前时间

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;

字符串
使用上面的代码,您可以找到当前时间,然后与特定时间进行比较。如果两个时间相等,则运行该函数。

u3r8eeie

u3r8eeie2#

我认为你可以使用这个代码。这不是你问的方式,但这是想法

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

字符串

8i9zcol2

8i9zcol23#

将你的函数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.");
 }

}

字符串

相关问题