Break function execution from another function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

Break function execution from another function

Post 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.
User avatar
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

Post by Christopher »

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

Post 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>
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Break function execution from another function

Post 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]
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

Post 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? :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Break function execution from another function

Post by VladSun »

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

Post 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.
Post Reply