how to terminate the script?

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Ktoeto
Posts: 4
Joined: October 13th, 2005, 5:04 am
Location: Moscow, Russia

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...
davestewart
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" :

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
Ktoeto
Posts: 4
Joined: October 13th, 2005, 5:04 am
Location: Moscow, Russia

O, that's great! Simple and efficient. Thanks!
davestewart
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:

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!
Post Reply