views:

64

answers:

6

I was wondering if there was a way to break javascript execution, something like this

<script>

if(already_done)
{
  return; //prevent execution (this yields an error)
}

doSomeStuff();

</script>

I know that this is possible like this:

<script>

if(already_done)
{
  // do nothing
}
else
{
  doSomeStuff();
}
</script>

But it's not the solution I'm looking for.

Hopefully this makes sense.

+2  A: 

Put your code in a closure, like this:

(function (){

  if(already_done){
    return;
  }

  doSomeStuff();
})();

It should work.

madeinstefano
This isn't necessarily a closure, it is an anonymous function though.
Nick Craver
@Nick Craver: When is it *not* a closure?
Tomalak
@Tomalak: A closure is when you return a function inside of another function, and in the inner function you're binding variables defined in the outer function per http://stackoverflow.com/questions/111102/how-does-a-javascript-closure-work
meder
@Tomalak - when nothing inside is it exposed to the outside, creating a linkage of sorts between internal an external members, "closing" off part of it, e.g. if it had `window.var = somethingInHere` and that referenced other things, it'd be a closure. There's often confusion around this, Closure != scoping != anonymous method, there's a lot of overlap, but they're not synonymous.
Nick Craver
@Nick Craver, @meder: Okay, fair enough.
Tomalak
+3  A: 

You have one option directly in a <script> block: you can throw an error, but this usually isn't desirable in the middle of a block of code...

if(already_done){
  throw "Uh oh";
}
Nick Craver
Yeah, I thought about that but I'm trying to avoid throwing an error (since it's technically not an error in my case)
Pablo Fernandez
@Pablo - If your case is general and directly in a block isn't a constraint, then I'd go the anonymous function route like @madeinstefano has above.
Nick Craver
Yes I'll guess I'll do that. Thanks anyway @Nick
Pablo Fernandez
+4  A: 

Wrap it in a function which immediately executes.

(function() {

    if (already_done) { return; }

    doSomeStuff();

})();

FYI: return is useless without being in a function context.

Also, this isn't a closure since it doesn't return an inner function which uses variables defined in an outer function.

meder
+1, but how about `(function() { if (!already_done) doSomeStuff(); })();`? ;-)
Tomalak
@Tomalak - I'm not sure what other things lie in his function, and it's probably best to be consistent with the OP.
meder
A: 

What would 'already_done' be? a boolean? it sounds to me like a loop... can you use something like?:

While ( !already_done ) { doSomeStuff(); }

Josmas
Not really. It would be more like the presence of a namespace that I've created in the `doSomeStuff()` function
Pablo Fernandez
A: 

You could use break with a label:

<script>

testcase:
if (already_done) {
    break testcase; //prevent execution
}

doSomeStuff();

</script>

though, as stated in other answers, wrapping it in a function is your best method.

jnpcl
A: 
if(!already_done){
    doSomeStuff();
}
Nico