Reputation: 103
I am trying to make a form to be posted using time. I have managed to make a count time. I would like to enter a specific time to post the from etc. 12:00. is it possible?
<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>
Upvotes: 1
Views: 419
Reputation: 323
This is done with find current time and then compare current time with your set time on that you want to post form
Find Current Time
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;
With above code you find the current time and then compare with specific time. If both time is equal then run the function.
Upvotes: 2
Reputation: 5410
Change your function countDown to this. First get the current time, then calc the remaining secs to the current time and set this as the timeout time in seconds
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.");
}
}
Upvotes: 1
Reputation: 2845
I think you can use this code. This is not exactly how you asked but this is the idea
<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>
Upvotes: 1