I have 3 js functions.
1. countdown() which counts down from a given number, updates a div and a hidden input with the current reaming time and when it reaches 0 it will submit a form
2. submitform() is called by countdown() when reaching 0, disables the submit button and submits a form
3. disable() is called on clicking the submit button, disables the submit button
First, is it a good idea to drop submitform() and just incorporate the disabling of the submitting button and the submitting of the form in countdown() when value is 0?
Second, how can I break countdown() from disable()? I had the fallowing situation when countdown was close to 0 and I clicked the submit button. Due to some lag, countdown reached 0 and submitted the form again.
If it's not clear what I meant I can also post the code.
Thanks.
Break function execution from another function
Moderator: General Moderators
-
andrei.mita
- Forum Commoner
- Posts: 65
- Joined: Sun May 08, 2005 4:06 am
- Location: Barlad/Romania
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Break function execution from another function
Please post your code. Probably you need to return a value that specifies whether the calling function should break or not.
(#10850)
-
andrei.mita
- Forum Commoner
- Posts: 65
- Joined: Sun May 08, 2005 4:06 am
- Location: Barlad/Romania
Re: Break function execution from another function
Here is the script and thanks
Code: Select all
<script type="text/javascript">
window.onload = function() {
startCountDown(17, 1000, myFunction);
}
function startCountDown(i, p, f) {
var pause = p; var fn = f; var countDownObj = document.getElementById("countDown");
if (countDownObj == null) { alert("div not found, check your id"); return;}
countDownObj.count = function(i) {
countDownObj.innerHTML = i; document.getElementById("speed").value= i;
if (i == 0 ) {
fn(); return;
}
setTimeout(function() {countDownObj.count(i - 1);},pause);
}
countDownObj.count(i);
}
function myFunction() {
document.game.B1.disabled=true;
document.game.B1.value="Wait...";
game.submit();
}
function DisableSubmit(){
document.game.B1.disabled=true;
document.game.B1.value="Wait...";
//here I want to break the execution of startCountDown()
}
</script>
<form name="game" method="POST" action="check.php">
//some inputs
<input type="submit" value="Answer" name="B1" onclick="DisableSubmit();">
</form>
Re: Break function execution from another function
You may use clearTimeout() or use a flag (boolean) to check against in countDownObj.count() function.
Untested!
[js]window.onload = function() { startCountDown(17, 1000, myFunction);} function startCountDown(i, p, f) { var pause = p; var fn = f; var countDownObj = document.getElementById("countDown"); if (countDownObj == null) { alert("div not found, check your id"); return;} countDownObj.enableSubmit = true; countDownObj.count = function(i) { if (!countDownObj.enableSubmit) return; countDownObj.innerHTML = i; document.getElementById("speed").value= i; if (i == 0 ) { fn(); return; } setTimeout(function() {countDownObj.count(i - 1);},pause); } countDownObj.count(i);} function myFunction() {document.game.B1.disabled=true;document.game.B1.value="Wait...";game.submit();} function DisableSubmit(){document.game.B1.disabled=true;document.game.B1.value="Wait...";//here I want to break the execution of startCountDown()var countDownObj = document.getElementById("countDown");countDownObj.enableSubmit = false;}[/js]
Untested!
[js]window.onload = function() { startCountDown(17, 1000, myFunction);} function startCountDown(i, p, f) { var pause = p; var fn = f; var countDownObj = document.getElementById("countDown"); if (countDownObj == null) { alert("div not found, check your id"); return;} countDownObj.enableSubmit = true; countDownObj.count = function(i) { if (!countDownObj.enableSubmit) return; countDownObj.innerHTML = i; document.getElementById("speed").value= i; if (i == 0 ) { fn(); return; } setTimeout(function() {countDownObj.count(i - 1);},pause); } countDownObj.count(i);} function myFunction() {document.game.B1.disabled=true;document.game.B1.value="Wait...";game.submit();} function DisableSubmit(){document.game.B1.disabled=true;document.game.B1.value="Wait...";//here I want to break the execution of startCountDown()var countDownObj = document.getElementById("countDown");countDownObj.enableSubmit = false;}[/js]
There are 10 types of people in this world, those who understand binary and those who don't
-
andrei.mita
- Forum Commoner
- Posts: 65
- Joined: Sun May 08, 2005 4:06 am
- Location: Barlad/Romania
Re: Break function execution from another function
Thanks Vlad. I will give it a try right away. One more question. I have two options for calling DisableSubmit(). I can either use onsubmit in the <form> or onclick in the <input type="submit">. Which is better? 
Re: Break function execution from another function
I think onsubmit() would be better. I remember I had some problems with disabling the submit button in IE (6?).
There are 10 types of people in this world, those who understand binary and those who don't
-
andrei.mita
- Forum Commoner
- Posts: 65
- Joined: Sun May 08, 2005 4:06 am
- Location: Barlad/Romania
Re: Break function execution from another function
That's exactly what I was afraid off. Thanks for clearing it out for me. The script works like a charm. Thanks a lot, really.