2

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.

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Put your code in a closure, like this:

(function (){

  if(already_done){
    return;
  }

  doSomeStuff();
})();

It should work.

5 Comments

This isn't necessarily a closure, it is an anonymous function though.
@Nick Craver: When is it not a closure?
@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 stackoverflow.com/questions/111102/…
@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, @meder: Okay, fair enough.
3

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";
}

3 Comments

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 - 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.
Yes I'll guess I'll do that. Thanks anyway @Nick
0

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

While ( !already_done ) { doSomeStuff(); }

1 Comment

Not really. It would be more like the presence of a namespace that I've created in the doSomeStuff() function
0

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.

1 Comment

This makes code hard to follow. If you want to halt to script, wrap everything in a self calling function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.