Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.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:setTimeout和多个提示_Javascript - Fatal编程技术网

Javascript:setTimeout和多个提示

Javascript:setTimeout和多个提示,javascript,Javascript,我有一个javascript代码,可以在页面启动时加载一个提示:你相信。。。 得到一个答案。无论用户输入什么,返回我看到的警报,等待10000毫秒,然后进入第二个提示。不知道我做错了什么。当我删除timeout函数及其下面的所有内容时,提示符工作得很好,但不确定如何使其余部分工作 <!DOCTYPE html> <html> <head> <title>T-Master, what drink would you like?</title&g

我有一个javascript代码,可以在页面启动时加载一个提示:你相信。。。
得到一个答案。无论用户输入什么,返回我看到的警报,等待10000毫秒,然后进入第二个提示。不知道我做错了什么。当我删除timeout函数及其下面的所有内容时,提示符工作得很好,但不确定如何使其余部分工作

<!DOCTYPE html>
<html>
<head>
<title>T-Master, what drink would you like?</title>
</head>

<body>


<script>
window.onload=first();

function first(){
var answer = prompt("Do you believe you have the power to change the world?");

switch(answer){
    default:
    alert("...I see");

setTimeout(function(){
    //do what you need here
}, 
10000);

}

var answer2 = prompt("Master, your drink?");
var text;

switch(answer2){
    case "Gatorade":
    text = "THat's what I thought sire";
    break;

    case "Orange Juice":
    text = "That's a good choice sir";
    break;

    case "Bliss"
    text = "Hmm, a finer choice than what I expected";
    break;

    case "nothing";
    text = "Very well sir";
    break;

    default:
    text = "I'll get on it";
    break;
}
alert(text);
}
</script>






</body>

</html>

这里混合了异步和同步编程。提示调用是sync,但setTimeout是异步的,将在随后的代码之后执行

window.onload=first();

function first() {
    var answer = prompt("Do you believe you have the power to change the world?");

    switch(answer) {
        default :
            alert("...I see");

            setTimeout(function() {
                //do what you need here
                var answer2 = prompt("Master, your drink?"),
                    text;

                switch(answer2) {
                    case "Gatorade" :
                        text = "That's what I thought sire";
                        break;
                    case "Orange Juice" :
                        text = "That's a good choice sir";
                        break;
                    case "Bliss" :
                        text = "Hmm, a finer choice than what I expected";
                        break;
                    case "nothing" :
                        text = "Very well sir";
                        break;
                    default :
                        text = "I'll get on it";
                        break;
                }

                alert(text);
            }, 10000);
    }
}

为什么要使用一个开关,然后除了使用默认值之外什么都不做?等待10000毫秒-不,它没有。这不是setTimeout所做的。然后进入第二个提示-很明显是这样。@Quentin很明显他是JavaScript新手,可能是编程新手。。。也许给他一个bone@Quentin如果你不知道如何帮助他们,也许就离他们远一点,打他们一顿,然后就开始行动。问题不在于这里,问题在于sync vs async。是的,他也有一些语法错误,你已经修复了,你也应该添加这些错误的解释:p代码有很多错误,需要告诉他所有的事情。相反,我把重点放在他面临的主要问题上,同步与异步。