Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用confirm()终止函数执行_Javascript_Conditional Statements - Fatal编程技术网

Javascript 使用confirm()终止函数执行

Javascript 使用confirm()终止函数执行,javascript,conditional-statements,Javascript,Conditional Statements,使用confirm()方法,在返回FALSE后,如何在没有条件的情况下停止函数执行?下面是一个示例调用和一个缩写函数: <form> <input value="Play" type="button" onClick="LetsPlay();"> </form> function LetsPlay() { confirm("Request permission to play Abracadabra!"); prompt("Pic

使用
confirm()
方法,在返回
FALSE
后,如何在没有条件的情况下停止函数执行?下面是一个示例调用和一个缩写函数:

<form>
    <input value="Play" type="button" onClick="LetsPlay();">
</form> 

function LetsPlay()
{

    confirm("Request permission to play Abracadabra!");
    prompt("Pick a number between 10 and 20");
    //more actions to follow
}

函数LetsPlay()
{
确认(“请求播放Abracadabra的许可!”);
提示(“选择10到20之间的数字”);
//更多后续行动
}
您可以使用和

如果confirm函数返回false,则它不必计算
&&
(短路逻辑)后面的表达式

如果将内部函数单独放置,则更容易阅读程序

function LetsPlay()
{
    confirm("Request permission to play Abracadabra!") && StartPlay();
}

function StartPlay() {
    prompt("Pick a number between 10 and 20");
    //more actions to follow
}

好了,现在问题已经回答了,你为什么要这么做同事挑战…在现实世界中,我总是使用有条件的!
function LetsPlay()
{
    confirm("Request permission to play Abracadabra!") && StartPlay();
}

function StartPlay() {
    prompt("Pick a number between 10 and 20");
    //more actions to follow
}