Tengo una función:
function myfunction() {
if (a == 'stop') // How can I stop the function here?
}
¿Hay algo como exit()
en JavaScript?
Puedes usar simplemente return
.
function myfunction() {
if(a == 'stop')
return;
}
Esto enviará un valor de retorno de undefined
a lo que sea que haya llamado a la función.
var x = myfunction();
console.log( x ); // console shows undefined
Por supuesto, puede especificar un valor de retorno diferente. Cualquier valor devuelto será registrado en la consola usando el ejemplo anterior.
return false;
return true;
return "some string";
return 12345;
si está buscando un script para evitar el envío del formulario cuando se encuentran algunos errores, este método debería funcionar
function verifyData(){
if (document.MyForm.FormInput.value.length == "") {
alert("Write something!");
}
else {
document.MyForm.submit();
}
}
cambiar el tipo de Botón de envío a "botón"";
<input value="Save" type="button" onClick="verifyData()">
Espero que esto ayude.