Jump to content

Exit Or Abort Function In Js?


skaterdav85

Recommended Posts

So here is my routine. I want to abort the script inside the first if statement, so that when you click once, the div is displayed, and when you click a 2nd time, it hides. But this doesnt work if you reset x equal to 0, because it just keeps the div displayed. And if you dont reset x to 0, the 3rd click (if you wanted to show the div again), wont do anything. Any ideas?<script language="javascript">var x = 0;function showBody() {if(x == 1) { document.getElementById('body').style.display='none'; x=0;//NEED EXIT OR ABORT FUNCTION HERE............................................... }if(x == 0) { document.getElementById('body').style.display='block'; x = 1;}}</script><div onclick="showBody();">click here</div><div id="body" style="display:none;">this is the hidden body</div>

Link to comment
Share on other sites

Just use return.function showBody() {if(x == 1) {document.getElementById('body').style.display='none';x=0;return;}...
what does return do? Ive never really been familiar with this, except the fact it can return a value. what does return alone do?
Link to comment
Share on other sites

It exits the function immediately. Doesn't matter if you're in a loop or whatever. Just BANG and you're out. Control passes to the statement that follows the statement that called the function in the first place.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...