Page 1 of 1

how to terminate the script?

Posted: October 13th, 2005, 5:21 am
by Ktoeto
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...

Posted: October 13th, 2005, 7:48 am
by davestewart
If you code your script as a function, you can exit it by using "return" :

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
Hope that helps :D

Posted: October 13th, 2005, 10:10 pm
by Ktoeto
O, that's great! Simple and efficient. Thanks!

Posted: October 14th, 2005, 4:30 am
by davestewart
No worries!

If you need to exit with an error code, or some other value, you can return a value:

Code: Select all

return myValue
For example:

Code: Select all

function myStuff(){
end=true
if(end==true)return "Script exited early!"
else return
}

var result=myStuff()
if(result!=undefined)alert(result)
Nice and easy!