SirChick Posted July 29, 2008 Share Posted July 29, 2008 Hey guys, I'm some what new to javascript so bare with me... I currently have submit button which can be pressed once every 3 seconds. So it disables it counts to 3 then allows it again. How ever this button when pressed needs to refresh the php page but the javascript is not allowing that to happen thus the form submit doesn't occur when pressed but the javascript runs every time. Is there a way to add a refresh prior to the count so the form will submit and the count also work? Here is what i have: <script type="text/javascript"> var c=0 var t function timedCount() { document.getElementById('txt').value=c; c=c+1; if (c>=4) { setTimeout("stopCount()"); } t=setTimeout("timedCount()",1000); } function stopCount() { clearTimeout(t); x="Submit"; c=0; document.getElementById('txt').value=x; } </script> In the form I have: <center> <form name="" method="POST" action="attack.php"> <input type="Button" id="txt" name="submit" value="Submit" onClick="timedCount()"> </form> </center> Quote Link to comment Share on other sites More sharing options...
obsidian Posted July 29, 2008 Share Posted July 29, 2008 Well, it sounds as though, if you're wanting your page to refresh when the button is clicked, your logic should be such that when the page refreshes, the button is inactive for three seconds. That way, when it is clicked and the form is submitted, it is automatically disabled for three seconds on page load again. I would try something like this: var c = 0; function setWait() { var btn = document.getElementById('txt'); if (c < 3) { btn.disabled = true; btn.value = 'Wait... ' + c; c++; setTimeout(setWait, 1000); } else { btn.disabled = false; btn.value = 'Attack!'; } } window.load = function() { setWait(); } Hope that helps. Quote Link to comment Share on other sites More sharing options...
SirChick Posted July 29, 2008 Author Share Posted July 29, 2008 Hmm that doesn't allow the page to refresh either.. do i have to change the submit button's html info to anything different? Quote Link to comment Share on other sites More sharing options...
obsidian Posted July 29, 2008 Share Posted July 29, 2008 Hmm that doesn't allow the page to refresh either.. do i have to change the submit button's html info to anything different? Heh... yea, my code is assuming your button is a submit type: <input type="submit" name="submit" value="Submit" /> Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 29, 2008 Share Posted July 29, 2008 Here is a simple way of doing it: <bodY onload="setTimeout('btnTest.disabled=false', 3000)"> <button id="btnTest" disabled="true" onclick="self.location.reload(true)">test</button> </body> Quote Link to comment Share on other sites More sharing options...
SirChick Posted July 29, 2008 Author Share Posted July 29, 2008 Hmm that doesn't allow the page to refresh either.. do i have to change the submit button's html info to anything different? Heh... yea, my code is assuming your button is a submit type: <input type="submit" name="submit" value="Submit" /> It is a submit type i changed it, though the page now refreshes the button itself doesn't count. This is what I have: JS: var c = 0; function setWait() { var btn = document.getElementById('txt'); if (c < 3) { btn.disabled = true; btn.value = 'Wait... ' + c; c++; setTimeout(setWait, 3000); } else { btn.disabled = false; btn.value = 'Attack'; } } window.load = function() { setWait(); } The form: <center> <form name="" method="POST" action="attack.php"> <input type="Submit" id="txt" name="attack" value="Attack"> </form> </center> Quote Link to comment Share on other sites More sharing options...
lemmin Posted July 29, 2008 Share Posted July 29, 2008 This is pretty much the same as Obsidian's code, but I tested it and it works fine. <html> <head> <script language="JScript"> var c=3; function setWait() { if (c) { frm1.btn.value="Wait..."+c; c--; setTimeout("setWait()", 1000); } else { frm1.btn.disabled=false; frm1.btn.value="Attack!"; } } </script> </head> <bodY onload="setWait()"> <form id="frm1"> <input type="submit" id="btn" disabled="true"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.