Hi all!
I have a newbie's trouble... How can I code terminating of script when some conditions occur? Is there an analog of 'break;' in loops, but for the entire script?
Sorry for my English - it's not my native language...
how to terminate the script?
Moderator: Paul Tuersley
-
- Posts: 114
- Joined: March 10th, 2005, 5:50 am
- Location: London, UK
- Contact:
If you code your script as a function, you can exit it by using "return" :
Hope that helps :D
Code: Select all
// set the function up
function myStuff(){
// do some stuff
// maybe a loop or 2
if(error==true)return // return (ie, exit the function) if a condition is met
}
myStuff() // run the function
-
- Posts: 114
- Joined: March 10th, 2005, 5:50 am
- Location: London, UK
- Contact:
No worries!
If you need to exit with an error code, or some other value, you can return a value:
For example:
Nice and easy!
If you need to exit with an error code, or some other value, you can return a value:
Code: Select all
return myValue
Code: Select all
function myStuff(){
end=true
if(end==true)return "Script exited early!"
else return
}
var result=myStuff()
if(result!=undefined)alert(result)