Page 1 of 1

Break function execution from another function

Posted: Wed Apr 15, 2009 4:15 am
by andrei.mita
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.

Re: Break function execution from another function

Posted: Wed Apr 15, 2009 12:25 pm
by Christopher
Please post your code. Probably you need to return a value that specifies whether the calling function should break or not.

Re: Break function execution from another function

Posted: Thu Apr 16, 2009 12:56 am
by andrei.mita
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

Posted: Thu Apr 16, 2009 2:26 am
by VladSun
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]

Re: Break function execution from another function

Posted: Thu Apr 16, 2009 2:38 am
by andrei.mita
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

Posted: Thu Apr 16, 2009 2:41 am
by VladSun
I think onsubmit() would be better. I remember I had some problems with disabling the submit button in IE (6?).

Re: Break function execution from another function

Posted: Thu Apr 16, 2009 2:51 am
by andrei.mita
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.