Reputation: 117
I'm trying to combine Submit form and date/localtime
submit from every time "15:30:00"
here goes my code js :
<script type="text/javascript">
function initClock()
{
var now = new Date();
var hr = now.getHours();
var min = now.getMinutes();
var sec = now.getSeconds();
if (min < 10) min = "0" + min;
if (sec < 10) sec = "0" + sec;
var time = document.getElementById('clockDisplay');
time.innerHTML = hr + ":" + min + ":" + sec;
setTimeout('initClock()', 500);
}
function timesubmit()
{
if (time.innerHTML == "15:30:00") // time submit 15:30:00
{
document.getElementById("myForm").submit();
}
else
{
//etc
}
}
</script>
and my HTML code goes like this:
<body onload="initClock()">
<div id="clockDisplay"></div>
<br>
<form id="myForm" action="http://example.com" method="post">
<input type="submit" onclick="timesubmit()" value="send" />
</form>
</body>
Any idea how I can get it to work? if time "15:30:00" auto submit form
Thanks for everybody who can help me :D
Upvotes: 0
Views: 450
Reputation: 11717
Check this Fiddle
HTML
<div id="clockDisplay"></div>
<br>
<form id="myForm" action="http://example.com" method="post">
<input type="button" onclick="timesubmit()" value="send" />
</form>
javascript
document.addEventListener("DOMContentLoaded",initClock,false);
var timeStamp;
function initClock()
{
var now = new Date();
var hr = now.getHours();
var min = now.getMinutes();
var sec = now.getSeconds();
if (hr < 10) hr = "0" + hr;
if (min < 10) min = "0" + min;
if (sec < 10) sec = "0" + sec;
timeStamp= hr + ":" + min + ":" + sec;
var time = document.getElementById('clockDisplay');
time.innerHTML = timeStamp;
timesubmit();
setTimeout(initClock, 0);
}
function timesubmit()
{
if (timeStamp == "15:30:00") // time submit 15:30:00
{
document.getElementById("myForm").submit();
}
else
{
//etc
}
}
Upvotes: 2
Reputation: 5340
Try this:
The changes are:
//so timesubmit can return false to stop the submit action, return true to continue.
onclick="return timesubmit()"
//'initClock()' seemed works fine too, but I prefer just initClock
//setTimeout(initClock, 500);
//define time in timesubmit function
var time = document.getElementById('clockDisplay');
Upvotes: 2